110 lines
3.4 KiB
C
110 lines
3.4 KiB
C
#include "gui.h"
|
|
#include <stdbool.h>
|
|
|
|
#include <SDL.h>
|
|
#include <SDL_image.h>
|
|
#include <SDL_ttf.h>
|
|
|
|
const SCREEN_WIDTH = 800;
|
|
const SCREEN_HEIGHT = 920;
|
|
|
|
SDL_Window* window = NULL;
|
|
// SDL_Surface* screenSurface = NULL;
|
|
SDL_Renderer *window_renderer = NULL;
|
|
SDL_Texture* tile_texture = NULL;
|
|
SDL_Texture* tile_textures[13];
|
|
SDL_Rect tile_size;
|
|
|
|
SDL_Texture *texture_from_png(char *n) {
|
|
SDL_Surface *s = IMG_Load(n);
|
|
SDL_Texture *t = SDL_CreateTextureFromSurface( window_renderer, s);
|
|
SDL_FreeSurface(s);
|
|
return t;
|
|
}
|
|
|
|
void gui_init() {
|
|
|
|
//Initialize SDL
|
|
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
|
|
{
|
|
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
|
|
}
|
|
|
|
window = SDL_CreateWindow( "2048", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
|
|
if( window == NULL )
|
|
{
|
|
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
|
|
}
|
|
// screenSurface = SDL_GetWindowSurface(window);
|
|
// SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 255, 255, 255));
|
|
|
|
|
|
// create renderer and render white window
|
|
window_renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
|
SDL_SetRenderDrawColor(window_renderer, 0xFF, 0xFF, 0xFF, 0xFF );
|
|
SDL_RenderClear(window_renderer);
|
|
SDL_RenderPresent(window_renderer);
|
|
// SDL_UpdateWindowSurface(window);
|
|
|
|
// load up textures
|
|
tile_texture = texture_from_png("res/tile.png");
|
|
tile_textures[0] = tile_textures;
|
|
tile_textures[1] = texture_from_png("res/2.png");
|
|
tile_textures[3] = texture_from_png("res/4.png");
|
|
tile_textures[4] = texture_from_png("res/8.png");
|
|
tile_textures[5] = texture_from_png("res/16.png");
|
|
tile_textures[6] = texture_from_png("res/32.png");
|
|
tile_textures[7] = texture_from_png("res/64.png");
|
|
tile_textures[8] = texture_from_png("res/128.png");
|
|
tile_textures[9] = texture_from_png("res/256.png");
|
|
tile_textures[10] = texture_from_png("res/512.png");
|
|
tile_textures[11] = texture_from_png("res/1024.png");
|
|
tile_textures[12] = texture_from_png("res/2048.png");
|
|
tile_size.x = 0;
|
|
tile_size.y = 0;
|
|
SDL_QueryTexture(tile_texture, NULL, NULL, &tile_size.w, &tile_size.h);
|
|
}
|
|
|
|
void gui_destroy() {
|
|
//Destroy window
|
|
SDL_DestroyWindow( window );
|
|
SDL_DestroyRenderer(window_renderer);
|
|
|
|
//Quit SDL subsystems
|
|
SDL_Quit();
|
|
|
|
}
|
|
|
|
void gui_loop(Game *game) {
|
|
bool quit = false;
|
|
bool needs_redraw = true; // there's no need to redraw window when nothing's changed. will be set to false later. first run has to be always done
|
|
SDL_Event e;
|
|
|
|
while(!quit) {
|
|
while( SDL_PollEvent( &e ) != 0 )
|
|
{
|
|
needs_redraw = true;
|
|
//User requests quit
|
|
if( e.type == SDL_QUIT )
|
|
{
|
|
quit = true;
|
|
}
|
|
}
|
|
|
|
if (needs_redraw) {
|
|
SDL_RenderClear(window_renderer);
|
|
SDL_Rect tile_rect = tile_size;
|
|
for (int i = 0; i < game->field_size_x; i++) {
|
|
tile_rect.x = i * 175;
|
|
for (int j = 0; j < game->field_size_y; j++) {
|
|
tile_rect.y = j * 175;
|
|
SDL_RenderCopy( window_renderer, tile_texture, NULL, &tile_rect);
|
|
}
|
|
}
|
|
SDL_RenderPresent(window_renderer);
|
|
needs_redraw = false;
|
|
}
|
|
|
|
}
|
|
|
|
} |