From ff8648d31fd678da56612a737106d178468d3b6e Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Tue, 29 Oct 2019 23:49:27 -0400 Subject: add image loading --- frontends/sdl/cetris_sdl.h | 2 ++ frontends/sdl/data/background.png | Bin 0 -> 1059116 bytes frontends/sdl/meson.build | 1 + frontends/sdl/sdl_ui.c | 20 ++++++++++++++++++++ 4 files changed, 23 insertions(+) create mode 100644 frontends/sdl/data/background.png (limited to 'frontends/sdl') diff --git a/frontends/sdl/cetris_sdl.h b/frontends/sdl/cetris_sdl.h index 02f7a13..c6779ac 100644 --- a/frontends/sdl/cetris_sdl.h +++ b/frontends/sdl/cetris_sdl.h @@ -47,5 +47,7 @@ typedef struct { SDL_Texture *main; SDL_Texture *queue; SDL_Texture *hold; + SDL_Texture *background; + SDL_Texture *game_background; } solo_game; diff --git a/frontends/sdl/data/background.png b/frontends/sdl/data/background.png new file mode 100644 index 0000000..1dc1448 Binary files /dev/null and b/frontends/sdl/data/background.png differ diff --git a/frontends/sdl/meson.build b/frontends/sdl/meson.build index a75c494..c4f56d9 100644 --- a/frontends/sdl/meson.build +++ b/frontends/sdl/meson.build @@ -12,6 +12,7 @@ else deps += compiler.find_library('m') deps += dependency('SDl2') deps += dependency('SDL2_ttf') + deps += dependency('SDL2_image') endif if host_machine.system() != 'windows' diff --git a/frontends/sdl/sdl_ui.c b/frontends/sdl/sdl_ui.c index ce2690e..ace4644 100644 --- a/frontends/sdl/sdl_ui.c +++ b/frontends/sdl/sdl_ui.c @@ -2,10 +2,12 @@ #ifdef _WIN32 #include #include +#include #define format_str sprintf_s #else #include #include +#include #define format_str snprintf #endif #include @@ -60,6 +62,8 @@ void setup_sdl() { SDL_LoadWAV(name, &(clear_sound[i].wav_spec), &(clear_sound[i].wav_buffer), &(clear_sound[i].wav_length)); } + IMG_Init(IMG_INIT_PNG); + SDL_LoadWAV("data/lock.wav", &(lock_sound.wav_spec), &(lock_sound.wav_buffer), &(lock_sound.wav_length)); audio_device = SDL_OpenAudioDevice(NULL, 0, &(clear_sound[0].wav_spec), NULL, 0); if (audio_device == 0) printf("failed to open audio device\n"); @@ -77,6 +81,11 @@ TTF_Font* get_font(int size) { return fonts[font_count++].font; } +SDL_Texture* load_png(char* file) { + SDL_Surface* loaded_surface = IMG_Load(file); + return SDL_CreateTextureFromSurface(render, loaded_surface); +} + void draw_text(char* string, int x, int y, TTF_Font* font, SDL_Color color) { SDL_Surface *surface; surface = TTF_RenderText_Solid(font, string, color); @@ -93,6 +102,11 @@ void draw_text(char* string, int x, int y, TTF_Font* font, SDL_Color color) { SDL_FreeSurface(surface); } +void draw_image(SDL_Texture *i, int x, int y, int width, int height) { + SDL_Rect dest = {x, y, width, height}; + SDL_RenderCopy(render, i, NULL, &dest); +} + void draw_block(int x, int y, int width, int height, SDL_Color c, SDL_Color off) { SDL_Rect b = {x + 1, y + 1, width - 1, height - 1}; SDL_SetRenderDrawColor(render, c.r, c.g, c.b, c.a); @@ -332,10 +346,12 @@ void draw(solo_game *s) { SDL_RenderClear(render); + draw_image(s->background, 0, 0, W, H); draw_board(s->main, &main_board, (W / 2) - 125, (H / 2) - 250, 250, 500); draw_held_piece(s->hold, &main_board, (W / 2) - 230, (H / 2) - 250, 100, 100); draw_piece_queue(s->queue, &main_board, (W / 2) + 130, (H / 2) - 250, 100, 450); draw_timer(&main_board, 20, 20); + draw_image(s->game_background, (W / 2) - 125, (H / 2) - 250, 250, 500); SDL_RenderPresent(render); } @@ -396,6 +412,10 @@ int main(void) { solo_game screen; load_solo(&screen); + screen.background = load_png("data/background.png"); + screen.game_background = load_png("data/loli.png"); + SDL_SetTextureBlendMode(screen.game_background, SDL_BLENDMODE_BLEND); + SDL_SetTextureAlphaMod(screen.game_background, 40); main_board.count_down = 3; init_game(&g, &config); -- cgit v1.2.3-101-g0448