My state doesn't work

Started by
4 comments, last by Robbie Woods 11 years, 10 months ago
Hey guys, im trying to make a state class, to keep my main clean and to make my code more flexible, but at the moment im having a small problem,

here is my main.cpp

#include <iostream>
#include <string>
#include "SDL.h"
#include "gState.h"
gState gameState;
using namespace std;
int main(int argc, char* args[] )
{
if( gameState.init() == false)
{
return 1;
}
gameState.run(1);
gameState.update();
return 0;
}


here is my gState.h

//Player header
#ifndef gSTATE_H
#define gSTATE_H
//includes
#include "gButton.h"
#include "gTimer.h"
#include "gButton.h"
#include <vector>
//Using namespace
//body
class gState{
private:
//Make a vector buttons
std::vector<gButton> buttons;
SDL_Surface *screen;
SDL_Event event;
gTimer fps;
int screenWidth;
int screenHeight;
int frameRate;
int currentState;
bool quit;
public:
gState();
void run(int state);
bool init();
int update();
bool rtnQuit(){return quit;}
};
//end body
#endif


and my gState.cpp

#include "gState.h"
gState::gState(){
}
bool gState::init()
{
quit = false;
frameRate = 60;
screen = NULL;
screenWidth = 1024;
screenHeight = 768;
if (SDL_Init( SDL_INIT_EVERYTHING) == -1)
{
return false;
}
screen = SDL_SetVideoMode(screenWidth, screenHeight, 32, SDL_SWSURFACE /*| SDL_FULLSCREEN*/);
if (screen == NULL)
{
return false;
}
SDL_WM_SetCaption("Phor V0.1", NULL);
return true;
}
void gState::run(int state)
{
currentState = state;
gButton newBtn(100,100,96,32);
buttons.push_back(newBtn);
}
int gState::update()
{
while (rtnQuit() == false)
{
fps.start();
//Event checker
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
quit = true;
}
buttons.at(0).handleEvents(event);
}
SDL_BlitSurface(buttons.at(0).draw(),NULL,screen,&buttons.at(0).rtnPos());
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,111,206,205));
if (SDL_Flip(screen) == -1)
{
return 1;
}
if( fps.rtnTicks() < 1000 / frameRate )
{
SDL_Delay( ( 1000 / frameRate ) - fps.rtnTicks() );
}
}
}



now when i run my code i get my window correct but the button doesnt come up, now i looked at the size of my vector and its like 400000 and my currentState isn't set to 1, its like set to 1281236, anyone know why its like picking up memory locations?

Canvas
Advertisement
Well if you are getting memory addresses instead of the data it points to, then that means you did something wrong with your pointers. I'll take a closer look.

I didn't see anything wrong with your state. And it's been awhile since I've done C++ but shouldn't your Vector be new'd up before you start pushing items to it?

gButton newBtn(100,100,96,32);
buttons.push_back(newBtn);


[strike]Nowhere in the code that I saw did you instantiate Vector. You declared it but that was it.[/strike]
So I see declaring the Vector is enough in C++

Beginner in Game Development?  Read here. And read here.

 

well its weird, it worked fine in main.cpp, but now i tried moving my code to a gState.cpp it gets the window fine but not the button...confused sad.png,
lol its ok,
i had some functions in the wrong place

SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,111,206,205));

SDL_BlitSurface(buttons.at(0).draw(),NULL,screen,&buttons.at(0).rtnPos());
was the other way round, but now i fixed it, it works!!!, I may have to take a break now, cheers for the help tho

using namespace std;
int main(int argc, char* args[] )
{
gState gameState;
if( gameState.init() == false)
{
return 1;
}
gameState.run(1);
gameState.update();
return 0;
}


Try that and tell me how it works out.

Beginner in Game Development?  Read here. And read here.

 


i had some functions in the wrong place

SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,111,206,205));

SDL_BlitSurface(buttons.at(0).draw(),NULL,screen,&buttons.at(0).rtnPos());
was the other way round, but now i fixed it, it works!!!, I may have to take a break now, cheers for the help tho

Ahh, I see. That makes sense, lol. Well I relearned something new today. :) Thanks!

Beginner in Game Development?  Read here. And read here.

 

Try using a constructor rather than an init function.

This topic is closed to new replies.

Advertisement