Valid_Dark: And... where exactly do you init SDL2?
Anyway, modification and threw in an actual init call: this runs (just getting window going)
#include "window.h"
int main (int argc, char **argv)
{
window mainwindow;
for (unsigned int i = 0; i < 5; i++)
{
mainwindow.update();
SDL_Delay(200);
}
return 0;
}
#include <stdio.h>
#include "SDL2/SDL.h"
#ifndef WINDOW_H
#define WINDOW_H
class window
{
private:
int screenw;
int screenh;
int screenbpp;
bool quit;
int FPS;
int frame;
int spriteframe;
int gamestate;
SDL_Window *Window;
SDL_Renderer *renderer;
bool windowOK;
bool windowed;
protected:
public:
window();
~window();
void init();
void update();
};
#endif // WINDOW_H
#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();
init();
//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();
}
*/
window::~window()
{
SDL_Quit();
}