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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Pornomag
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 41
Joined: Tue Jun 21, 2011 5:39 am
Programming Language of Choice: C++

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

Post 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
Last edited by Pornomag on Tue Jul 19, 2011 8:08 am, edited 1 time in total.
User avatar
THe Floating Brain
Chaos Rift Junior
Chaos Rift Junior
Posts: 284
Joined: Tue Dec 28, 2010 7:22 pm
Current Project: RTS possible Third Person shooter engine.
Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
Programming Language of Choice: C/C++, Python 3, C#
Location: U.S

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

Post 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)
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself

ImageImage
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
THe Floating Brain
Chaos Rift Junior
Chaos Rift Junior
Posts: 284
Joined: Tue Dec 28, 2010 7:22 pm
Current Project: RTS possible Third Person shooter engine.
Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
Programming Language of Choice: C/C++, Python 3, C#
Location: U.S

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

Post 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.
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself

ImageImage
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

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

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Pornomag
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 41
Joined: Tue Jun 21, 2011 5:39 am
Programming Language of Choice: C++

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

Post 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.
Pornomag
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 41
Joined: Tue Jun 21, 2011 5:39 am
Programming Language of Choice: C++

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

Post 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
User avatar
THe Floating Brain
Chaos Rift Junior
Chaos Rift Junior
Posts: 284
Joined: Tue Dec 28, 2010 7:22 pm
Current Project: RTS possible Third Person shooter engine.
Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
Programming Language of Choice: C/C++, Python 3, C#
Location: U.S

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

Post 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 :-)
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself

ImageImage
Post Reply