So, I'm having some trouble with SDL2
I'm porting over a project, and right now I just want it to draw everything to a surface like it was in SDL1.2, and then apply that surface to a texture and put the texture on the screen.
I know doing it this way isn't going to give me any sort of performance boost, but I don't need a performance boost,
my main project that is being ported is just a menu system so far, so I'm planning on porting the menu system that way, then doing the rest of the game with actual hardware acceleration.
So right now I'm porting over a platformer engine I made, once I get it working in its current state, I'm going to add to it to make it into a full mini game.
Anyways, here is my code, if anyone has experience with SDL2, can you give it a look and let me know if you can figure out what is causing it to crash?
Here is the code for window.cpp, I'm 98% sure the problem is happening in here, I've read through the documentation, and forums, and migration guides, I can't figure out whats causing it to crash.
#include "window.h"
#include "timer.h"
#include "menu.h"
#include "options.h"
#include "game.h"
#include <stdio.h>
window::window()
{
screenw = 1024; //32 wide
screenh = 768; //24 tall
screenbpp = 32;
keydown = SDL_GetKeyboardState(NULL);
quit = false;
FPS = 30;
frame = 0;
spriteframe = 0;
gamestate = 0;
loadicon();
screen = SDL_CreateRGBSurface(0, screenw, screenh, screenbpp,
0x00FF0000,
0x0000FF00,
0x000000FF,
0xFF000000);
if( screen == NULL )
{
printf( "Unable to init. screen" );
}
Window = SDL_CreateWindow("Running Man",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
screenw, screenh,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
if( Window == NULL )
{
printf( "Unable to init. Window" );
}
renderer = SDL_CreateRenderer(Window, -1, 0);
if( renderer == NULL )
{
printf( "Unable to init. renderer" );
}
ScreenTex = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
screenw, screenh);
if( ScreenTex == NULL )
{
printf( "Unable to init. screen texture" );
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
if( screen == NULL ) //If there's an error
{
windowOK = false;
return;
}
else
{
windowOK = true;
}
windowed = true; //Set window flag
}
void window::loadicon()
{
//SDL_Surface* icon = Load_PNG("GUI/icon");
// SDL_WM_SetIcon(icon,NULL);
// SDL_FreeSurface(icon);
}
void window::toggle_fullscreen()
{
if( windowed == true ) //If the screen is windowed
{
SDL_SetWindowFullscreen( Window, SDL_FALSE ); //Set the screen to fullscreen
if( screen == NULL ) //If there's an error
{
windowOK = false;
return;
}
windowed = false; //Set the window state flag
}
else if( windowed == false ) //If the screen is fullscreen
{
SDL_SetWindowFullscreen( Window, SDL_TRUE ); //Window the screen
if( screen == NULL ) //If there's an error
{
windowOK = false;
return;
}
windowed = true; //Set the window state flag
}
}
void window::update()
{
SDL_UpdateTexture(ScreenTex, NULL, screen->pixels, screen->pitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, ScreenTex, NULL, NULL);
SDL_RenderPresent(renderer);
//SDL_Flip( screen );
frame++;
if ((frame%2)==0)
spriteframe++;
}
void window::init()
{
SDL_Init ( SDL_INIT_EVERYTHING );
TTF_Init();
}
void window::handle_events()
{
if( windowOK == false ) //If there's something wrong with the window
{
return;
}
if( event.type == SDL_QUIT ) //If the user has Xed out the window
{
quit = true; //Quit the program
}
if( event.type == SDL_WINDOWEVENT_SIZE_CHANGED ) //If the window resized
{
screenw = event.window.data1;
screenh = event.window.data2;
SDL_RenderPresent( renderer );
if( screen == NULL ) //If there's an error
{
windowOK = false;
return;
}
}
else if ((keydown[SDLK_LALT] || keydown[SDLK_RALT]) && keydown[SDLK_RETURN] ) //ALT+Enter goes to Fullscreen.
{
toggle_fullscreen(); //Turn fullscreen on/off
}
else if ((keydown[SDLK_LALT] || keydown[SDLK_RALT]) && keydown[SDLK_F4] ) //ALT+F4 quits.
{
quit = true; //quits.
}
if( ( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_ESCAPE ) )
{
//Quit the program
quit = true;
}
//If the window focus changed
//Here is sthe stuff for if windows focus changed, window loses focus, etc. Code changed for SDL2, haven't redone yet.
}
bool window::error()
{
return !windowOK;
}
void window::gameloop()
{
Timer fps; //start FPS timer, keeps it running at constant fps.
menu mainMenu;
options mainOptions;
game mGame;
while( quit == false ) //MAIN GAME LOOP
{
fps.start();
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
handle_events(); //Handle window events
//GAME CONTROLS GO HERE
if (gamestate == 0) //before main menu.
{
}
if (gamestate == 1) //Main Menu
{
mainMenu.handleEvents(event);
if (mainMenu.select() == 1)
{
gamestate = 2;
mainMenu.selected = 0;
}
if ((mainMenu.select() == 0) && gamestate == 1)
{
gamestate = 3;
mainMenu.selected = 0;
}
}
if (gamestate == 2) //Main Menu
{
mainOptions.handleEvents(event);
if (mainOptions.select() == 1)
{
gamestate = 1;
mainOptions.selected = 0;
}
}
}
if (gamestate == 0) //before main menu.
{
//show company logos
gamestate++;
}
if (gamestate == 1) //Main Menu
{
mainMenu.showmenu(screen);
}
if (gamestate == 2) //Main Options
{
mainOptions.showmenu(screen);
}
if (gamestate == 3) //Game Screen
{
mGame.playlevel(screen, spriteframe);
}
//Update the screen
update();
if ( fps.get_ticks() < 1000 / FPS)
{
SDL_Delay( ( 1000 / FPS ) - fps.get_ticks() );
}
// 1000/fps.get_ticks(); // gives the framerate of the last/current frame, should be checked ever x seconds, and stored in a variable to check framerate, if framerate checking should be desired.
}
}
void window::quitall()
{
SDL_Quit();
TTF_Quit();
}