diff --git a/game_private.c b/game_private.c index 3f4526f..b22704b 100644 --- a/game_private.c +++ b/game_private.c @@ -13,6 +13,12 @@ unsigned int min(unsigned int a, unsigned int b) { return b; } +int int_min(int a, int b) { + if (a < b) + return a; + return b; +} + void checkPointer(void* p, char* errMessage) { if (!p) { fprintf(stderr, "%s", errMessage); diff --git a/game_private.h b/game_private.h index 3348f18..da1e03a 100644 --- a/game_private.h +++ b/game_private.h @@ -30,6 +30,15 @@ unsigned int max(unsigned int a, unsigned int b); */ unsigned int min(unsigned int a, unsigned int b); +/** + * @brief return the min between a and b + * + * @param a int to test + * @param b int to test + * @return the min between a and b + */ +int int_min(int a, int b); + /** * @brief check if the pointer is NULL fprinf and exit else nothing * diff --git a/model.c b/model.c index 1dc95df..f39f724 100644 --- a/model.c +++ b/model.c @@ -7,11 +7,22 @@ #include #include #include +#include "game.h" +#include "game_aux.h" +#include "game_ext.h" +#include "game_private.h" +#include "game_tools.h" /* **************************************************************** */ struct Env_t { game g; + SDL_Texture** zero; // array of two texture one white and one red for each number (red color = r:255 g:50 b:50) + SDL_Texture** one; + SDL_Texture** two; + SDL_Texture** three; + SDL_Texture** four; + SDL_Texture** lightbulb; // array of the two lightbulb images first white second red SDL_Texture* but_restart_up; SDL_Texture* but_restart_down; SDL_Texture* but_undo_up; @@ -34,7 +45,111 @@ Env* init(SDL_Window* win, SDL_Renderer* ren, int argc, char* argv[]) { /* **************************************************************** */ -void render(SDL_Window* win, SDL_Renderer* ren, Env* env) { /* PUT YOUR CODE HERE TO RENDER TEXTURES, ... */ +void render_blank(SDL_Renderer* ren, SDL_Rect* rec, bool lighted) { + if (lighted) { + SDL_SetRenderDrawColor(ren, 255, 255, 0, SDL_ALPHA_OPAQUE); + SDL_RenderFillRect(ren, rec); + } + SDL_SetRenderDrawColor(ren, 127, 127, 127, SDL_ALPHA_OPAQUE); + SDL_RenderDrawRect(ren, rec); +} + +void render_wall(SDL_Renderer* ren, Env* env, SDL_Rect* rec, int nb, bool error) { + SDL_Rect number; + SDL_SetRenderDrawColor(ren, 0, 0, 0, SDL_ALPHA_OPAQUE); + SDL_RenderFillRect(ren, rec); + switch (nb) { + case 0: + SDL_QueryTexture(env->zero[error ? 1 : 0], NULL, NULL, &number.w, &number.h); + number.x = rec->x + rec->w / 2 - number.x / 2; + number.y = rec->y + rec->h / 2 - number.y / 2; + SDL_RenderCopy(ren, env->zero[error ? 1 : 0], NULL, &number); + break; + case 1: + SDL_QueryTexture(env->one[error ? 1 : 0], NULL, NULL, &number.w, &number.h); + number.x = rec->x + rec->w / 2 - number.x / 2; + number.y = rec->y + rec->h / 2 - number.y / 2; + SDL_RenderCopy(ren, env->one[error ? 1 : 0], NULL, &number); + break; + case 2: + SDL_QueryTexture(env->two[error ? 1 : 0], NULL, NULL, &number.w, &number.h); + number.x = rec->x + rec->w / 2 - number.x / 2; + number.y = rec->y + rec->h / 2 - number.y / 2; + SDL_RenderCopy(ren, env->two[error ? 1 : 0], NULL, &number); + break; + case 3: + SDL_QueryTexture(env->three[error ? 1 : 0], NULL, NULL, &number.w, &number.h); + number.x = rec->x + rec->w / 2 - number.x / 2; + number.y = rec->y + rec->h / 2 - number.y / 2; + SDL_RenderCopy(ren, env->three[error ? 1 : 0], NULL, &number); + break; + case 4: + SDL_QueryTexture(env->four[error ? 1 : 0], NULL, NULL, &number.w, &number.h); + number.x = rec->x + rec->w / 2 - number.x / 2; + number.y = rec->y + rec->h / 2 - number.y / 2; + SDL_RenderCopy(ren, env->four[error ? 1 : 0], NULL, &number); + break; + } +} + +void render_lightbulb(SDL_Renderer* ren, SDL_Rect* rec, SDL_Texture* lightbulb_texture) { + SDL_Rect lightbulb; + SDL_SetRenderDrawColor(ren, 255, 255, 0, SDL_ALPHA_OPAQUE); + SDL_RenderFillRect(ren, rec); + SDL_SetRenderDrawColor(ren, 127, 127, 127, SDL_ALPHA_OPAQUE); + SDL_RenderDrawRect(ren, rec); + lightbulb.h = rec->h - rec->h / 10; + lightbulb.w = rec->w - rec->w / 10; + lightbulb.x = rec->x + rec->w / 2 - lightbulb.w / 2; + lightbulb.y = rec->y + rec->h / 2 - lightbulb.h / 2; + SDL_RenderCopy(ren, lightbulb_texture, NULL, &lightbulb); +} + +void render_mark(SDL_Renderer* ren, SDL_Rect* rec, bool lighted) { + if (lighted) + SDL_SetRenderDrawColor(ren, 255, 255, 0, SDL_ALPHA_OPAQUE); + else + SDL_SetRenderDrawColor(ren, 255, 255, 255, SDL_ALPHA_OPAQUE); + SDL_RenderFillRect(ren, rec); + SDL_SetRenderDrawColor(ren, 127, 127, 127, SDL_ALPHA_OPAQUE); + SDL_RenderDrawRect(ren, rec); + SDL_SetRenderDrawColor(ren, 0, 0, 0, SDL_ALPHA_OPAQUE); + SDL_Rect mark; + mark.h = rec->h / 4; + mark.w = rec->w / 4; + mark.x = rec->x + rec->w / 2 - mark.w / 2; + mark.y = rec->y + rec->h / 2 - mark.h / 2; + SDL_RenderFillRect(ren, &mark); +} + +void render(SDL_Window* win, SDL_Renderer* ren, Env* env) { + int w, h; + SDL_GetWindowSize(win, &w, &h); + // set background color + SDL_SetRenderDrawColor(ren, 255, 255, 255, SDL_ALPHA_OPAQUE); + SDL_RenderClear(ren); + SDL_Rect rec; + int size_rec = int_min(w / game_nb_cols(env->g), h / game_nb_rows(env->g)); + rec.h = size_rec; + rec.w = size_rec; + int rec_x = w / 2 - size_rec * game_nb_cols(env->g) / 2; + int rec_y = h / 2 - size_rec * game_nb_rows(env->g) / 2; + rec.x = rec_x; + rec.y = rec_y; + for (uint i = 0; i < game_nb_rows(env->g); i++) { + for (uint j = 0; j < game_nb_cols(env->g); j++) { + if (game_is_lightbulb(env->g, i, j)) + render_lightbulb(ren, &rec, env->lightbulb[game_has_error(env->g, i, j) ? 1 : 0]); + else if (game_is_black(env->g, i, j)) + render_wall(ren, env, &rec, game_get_black_number(env->g, i, j), game_has_error(env->g, i, j)); + else if (game_is_blank(env->g, i, j)) + render_blank(ren, &rec, game_is_lighted(env->g, i, j)); + else if (game_is_marked(env->g, i, j)) + render_mark(ren, &rec, game_is_lighted(env->g, i, j)); + } + } + rec.x = rec_x; + rec.y += size_rec; } /* **************************************************************** */ @@ -44,21 +159,18 @@ bool process(SDL_Window* win, SDL_Renderer* ren, Env* env, SDL_Event* e) { SDL_GetWindowSize(win, &w, &h); if (e->type == SDL_QUIT) { - return true; - } + return true; + } #ifdef _ANDROID_ #else - else if (e->type == SDL_WINDOWEVENT){ - init(win, ren , w, h/*(w, h) a changer*/); - } - else if (e->type == SDL_SDL_MOUSEMOTION){ + else if (e->type == SDL_WINDOWEVENT) { + init(win, ren, w, h /*(w, h) a changer*/); + } else if (e->type == SDL_MOUSEMOTION) { SDL_Point mouse; SDL_GetMouseState(&mouse.x, &mouse.y); - } - else if (e->type == SDL_MOUSEBUTTONDOWN){ - + } else if (e->type == SDL_MOUSEBUTTONDOWN) { } #endif return false; diff --git a/model.h b/model.h index be9cfad..3856082 100644 --- a/model.h +++ b/model.h @@ -3,7 +3,7 @@ #ifndef MODEL_H #define MODEL_H -#include +#include #include #include