[C++ SDL] Is there anything wrong with my game loop?[SOLVED]
Posted: Mon Jun 27, 2011 4:04 am
Okay so I've just tried making some menus with my game loop but I got the buttons to work just fine before but now, it doesn't show the image when I click the button, it seems to be loading quite fine. Is there anything wrong with the game loop, or is there any easier way to make menus in SDL?
My main.cpp
and the actual APP class, which is derived from a system class...
the .h for APP
the .cpp for APP
and also the .h for the system class:
My main.cpp
Code: Select all
#include "miguelsengine.h"
int main(int argc, char *args[])
{
APP main_game;
//load the settings
main_game.load_file("settings/window_info.txt");
//system set up...
main_game.Setup();
return main_game.main_loop();
}
the .h for APP
Code: Select all
#pragma once
#include "System.h"
#include "Timer.h"
class APP : public System{
Char pone;
Tile BG;
Timer fps;
Button bPlay;
Button bExit;
int menuState;
public:
APP();
//inits everything (loads)
bool Init();
//single frame for menu..
int menu();
//single frame for game..
int game();
//clears screen
bool clear_screen();
//exits the game (clears everything up, sets Done to true)
void exit();
//handles the input...
void handle_menu_input();
void handle_game_input();
//the main loop...
virtual int main_loop();
};
Code: Select all
#include "APP.h"
APP::APP(){
menuState = 0;
}
bool APP::Init(){
//player set up...
if(!pone.setup("images/sprites/AWESOME.png", 32, 48, false, true)){
fprintf(stderr,"ERROR: cannot load image for player\n");
return false;
}
//set poisition and collision for player...
pone.load_info("settings/pone_settings.txt");
//pone.set_collision(0,0,32,48);
// background setup...
if(!BG.setup("images/sprites/grassandsticks.png", 32, 32, false, true)){
fprintf(stderr,"ERROR: cannot load background image\n");
return false;
}
bPlay.load_info("settings/play_button_info.txt");
bExit.load_info("settings/exit_button_info.txt");
fps.start();
return true;
}
int APP::menu(){
if(!bPlay.Draw(screen)){
return 3;
}
if(!bExit.Draw(screen)){
return 4;
}
return 0;
}
int APP::game(){
pone.move();
if(!pone.Draw(screen, pone.get_frameno())){
return 3;
}
return 0;
}
bool APP::clear_screen(){
SDL_Rect temp;
temp.x = 0;
temp.y = 0;
temp.w = get_screen_w();
temp.h = get_screen_h();
if(SDL_FillRect(screen,&temp,SDL_MapRGB(screen->format,0,0,0)) == -1){
return false;
}
return true;
}
//exit function
void APP::exit(){
pone.destroy();
BG.destroy();
bPlay.destroy();
bExit.destroy();
}
//handle input functions
void APP::handle_game_input(){
pone.handle_input(&in_event);
}
void APP::handle_menu_input(){
if(in_event.type == SDL_MOUSEMOTION){
bPlay.forceUpdateButtonIMG(&in_event);
bExit.forceUpdateButtonIMG(&in_event);
}
}
//main loop
int APP::main_loop(){
if(!Init()){
//first error that could occur...
return 1;
}
int error = 0;
for(int frame = 0; GetDone() != true; frame++){
//poll event...
while(SDL_PollEvent(&in_event)){
handle_input(&in_event);
if(menuState == 0){
handle_menu_input();
}else if(menuState == 1){
handle_game_input();
}
}
if(menuState == 0){
if(error = menu() != 0){
break;
}
}else if(menuState == 1){
if(error = game() != 0){
break;
}
}else{
break;
}
RenderScreen();
//limit the frames per second its drawn...
if(fps.get_ticks() < 1000 / FRAMES_PER_SECOND){
SDL_Delay( (1000 / FRAMES_PER_SECOND) - fps.get_ticks() );
}
//reset the frame number...
if(frame >= FRAMES_PER_SECOND) frame = 0;
frame++;
}
exit();
return 0;
}
Code: Select all
#pragma once
#ifndef SYSTEM_H
#define SYSTEM_H
#include "sdlstuff.h"
#include "input.h"
#include "IMG.h"
#include "Crop.h"
#include "Tile.h"
#include "Physics.h"
#include "anim.h"
#include "Character.h"
#include "Button.h"
#include <fstream>
#include <string>
class System : public Input
{
protected:
int screen_w, screen_h, screen_bpp;
bool fullscreen, Done;
SDL_Event in_event;
SDL_Surface* screen;
std::string window_text;
public:
//default constructor
System(void);
System(const System &Obj);
//custom constructor from a file...
System(std::string filename);
//custom constructor
System(int w, int h, int bpp, bool fs = false);
//virtual default destructor
virtual ~System(void);
//quits the application by calling SDL_Quit() and other things
void quit();
//sets up the screen and etc...
bool Setup();
bool Setup(char* titlebar, bool fs = false);
//sets up the screen depending if its fullscreen or not...
bool changescreen();
//draws the screen to the window
bool RenderScreen();
//virtual functions...
//handles input
virtual void handle_input(SDL_Event *event);
virtual int main_loop(){
//pure virtual
return 0;
}
//getters
int get_screen_w();
int get_screen_h();
int get_screen_bpp();
bool GetDone();
std::string get_window_text();
//setters
void set_screen_w(int w);
void set_screen_h(int h);
void set_screen_bpp(int bpp);
void set_Done(bool B = true);
void set_fullscreen(bool f = true);
//window_text functions
void set_window_text(std::string text);
void update_window_title();
//load function
bool load_file(std::string filename);
};
#endif