Page 1 of 1

[C++ SDL] Is there anything wrong with my game loop?[SOLVED]

Posted: Mon Jun 27, 2011 4:04 am
by Pornomag
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

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();
}
and the actual APP class, which is derived from a system class...

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();
};
the .cpp for APP

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;
}
and also the .h for the system class:

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

Re: [C++ SDL] Is there anything wrong with my game loop?

Posted: Fri Jul 15, 2011 12:24 pm
by THe Floating Brain
I mean this in the nicest way possible ( a little constructive criticism if you will ):
You need to learn to debug on your own. The reason no one else has responded to your post is because it is A: A problem that you can fix yourself and B: You could probably research it on Google and C: no one wants to read through all that code for a problem you can probably fix yourself. Just like you got buttons to work you need to get the rest to work the same way. (debugging)

Re: [C++ SDL] Is there anything wrong with my game loop?

Posted: Fri Jul 15, 2011 10:11 pm
by Ginto8
THe Floating Brain wrote:I mean this in the nicest way possible ( a little constructive criticism if you will ):
You need to learn to debug on your own. The reason no one else has responded to your post is because it is A: A problem that you can fix yourself and B: You could probably research it on Google and C: no one wants to read through all that code for a problem you can probably fix yourself. Just like you got buttons to work you need to get the rest to work the same way. (debugging)
what with the N.A.N. thread, you're one to talk.

The issue exists in the RenderScreen() function, not in the game loop function. I don't know where that function is or how it's implemented, so that's all I can tell you.

Re: [C++ SDL] Is there anything wrong with my game loop?

Posted: Sun Jul 17, 2011 12:45 pm
by THe Floating Brain
Ginto8 wrote:
THe Floating Brain wrote:I mean this in the nicest way possible ( a little constructive criticism if you will ):
You need to learn to debug on your own. The reason no one else has responded to your post is because it is A: A problem that you can fix yourself and B: You could probably research it on Google and C: no one wants to read through all that code for a problem you can probably fix yourself. Just like you got buttons to work you need to get the rest to work the same way. (debugging)
what with the N.A.N. thread, you're one to talk.
Maybe I deserved that :lol: . But I will tell you the reason I said what I did.
I am recalling myself back about a year ago when I couldn't even make a function correctly. For every error that did not involve a missing semicolon I would make a post. People started to get mad at me. Mainly when I started posting run time errors. I would look at my program and get caught up in my own ignorance and assume I was correct and the IDE was wrong. Then I realized I needed to do things on my own. Seeing as Pornomag seems to have an idea at to propper debugging I was attempting to not have him end up like myself a year later re-writing everything like I did.

Re: [C++ SDL] Is there anything wrong with my game loop?

Posted: Mon Jul 18, 2011 4:37 pm
by dandymcgee
THe Floating Brain wrote: Maybe I deserved that :lol: . But I will tell you the reason I said what I did.
I am recalling myself back about a week ago when I couldn't even make a function correctly. For every error that did not involve a missing semicolon I would make a post. People started to get mad at me. Mainly when I started posting run time errors. I would look at my program and get caught up in my own ignorance and assume I was correct and the IDE was wrong. Then I realized I needed to do things on my own. Seeing as Pornomag seems to have an idea at to propper debugging I was attempting to not have him end up like myself a year later re-writing everything like I did.
Fix'd. ;)

In all seriousness, although what he says may seem a bit hypocritical it is also good advice.

Re: [C++ SDL] Is there anything wrong with my game loop?

Posted: Tue Jul 19, 2011 7:40 am
by Pornomag
Sorry if I seem like a noob, but there nothing wrong with it error wise, I was just curious if this is how you should actually make you game loop like for menu's and stuff.
But now I'm pretty sure I have a right idea how to do menus and stuff with Polymorphism. I was just an idiot and didn't know if there was an easier way to do this, once again I'm sorry.

Re: [C++ SDL] Is there anything wrong with my game loop?

Posted: Tue Jul 19, 2011 7:41 am
by Pornomag
Oh and it I couldn't click it because in my Button class I was checking if(event.type == SDL_MOUSEMOTION) then checking if the user clicked the mouse... Silly me :P

Re: [C++ SDL] Is there anything wrong with my game loop?

Posted: Tue Jul 19, 2011 7:23 pm
by THe Floating Brain
Pornomag wrote:Sorry if I seem like a noob, but there nothing wrong with it error wise, I was just curious if this is how you should actually make you game loop like for menu's and stuff.
But now I'm pretty sure I have a right idea how to do menus and stuff with Polymorphism. I was just an idiot and didn't know if there was an easier way to do this, once again I'm sorry.
Your not a idiot; also don't be sorry, you asked a question and I was a bit hard on you.
Pornomag wrote:Oh and it I couldn't click it because in my Button class I was checking if(event.type == SDL_MOUSEMOTION) then checking if the user clicked the mouse... Silly me :P
There ya' go :-)