From dc9b56a7650dd65d574c0e33ea7cb9e1612622f7 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Mon, 7 Jan 2019 21:59:53 +0100 Subject: Start porting code to SDL2 (WIP) --- main.c | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 5561051..2a458d6 100644 --- a/main.c +++ b/main.c @@ -53,16 +53,19 @@ int main(int argc, char *argv[]) if ( SDL_Init( SDL_INIT_VIDEO ) == -1 ) error_sdl("Could not initialise SDL"); atexit(SDL_Quit); - // Set surf_screen - SDL_Surface* surf_screen = NULL; SDL_Window * sdlWindow = SDL_CreateWindow("EchidneaMenace", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL ); - if ( surf_screen == NULL ) error_sdl("Could not set videomode"); SDL_Renderer *renderer = SDL_CreateRenderer(sdlWindow, -1, 0); + + // Set surf_screen + SDL_Texture* surf_screen = SDL_CreateTexture(renderer, + SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, + 640, 480); + if ( surf_screen == NULL ) error_sdl("Could not set videomode"); printf("done\n"); - /// Menu printf(" ==== Entered menu\n"); @@ -70,8 +73,20 @@ int main(int argc, char *argv[]) SDL_Event menuevent; // Load the menu graphic into memory SDL_Surface *surf_title = IMG_Load( "titlescreen.png" ); + if (!surf_title) { + error_sdl("could not get title surface\n"); + } + + SDL_Texture *surf_title_tex = SDL_CreateTextureFromSurface(renderer, surf_title); + if (!surf_title_tex) { + error_sdl("could not get title texture from surface\n"); + } - + int w, h; + Uint32 format; + SDL_QueryTexture(surf_title_tex, &format, NULL, &w, &h); + printf("w: %d, h: %d, format: %s\n", w, h, SDL_GetPixelFormatName(format)); + /* We loop the menu continously for a number of reasons: * 1) To make sure we capture and respond to user input * 2) To continue redrawing the menu graphic. @@ -81,8 +96,14 @@ int main(int argc, char *argv[]) */ while ( 1 ) { - SDL_BlitSurface( surf_title, NULL, surf_screen, NULL); // Draw menu - SDL_Flip( surf_screen ); // Update screen + + SDL_UpdateTexture(surf_screen, NULL, surf_title->pixels, surf_title->pitch); + + //SDL_BlitSurface( surf_title, NULL, surf_screen, NULL); // Draw menu + + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, surf_screen, NULL, NULL); + SDL_RenderPresent(renderer); // The while statement attempts to go through every key the user has pressed // it stops when the user has not pressed any more keys @@ -92,8 +113,8 @@ int main(int argc, char *argv[]) if ( menuevent.type == SDL_KEYDOWN ) { // Menu choice selection - if ( menuevent.key.keysym.sym == SDLK_p ) game_loop( surf_screen ); - else if ( menuevent.key.keysym.sym == SDLK_h ) help( surf_screen ); + if ( menuevent.key.keysym.sym == SDLK_p ) game_loop(renderer, surf_screen); + else if ( menuevent.key.keysym.sym == SDLK_h ) help(renderer, surf_screen); else if ( menuevent.key.keysym.sym == SDLK_q || menuevent.key.keysym.sym == SDLK_ESCAPE ) exit(0); } } @@ -103,9 +124,6 @@ int main(int argc, char *argv[]) SDL_Delay( 200 ); } - - - /* Technically the computer will never reach this point, because the while statement * above will run forever until the user quits the game. * @@ -113,5 +131,4 @@ int main(int argc, char *argv[]) */ return 0; - } -- cgit v1.2.1-18-gbd029