Sign in to follow this  
Canvas

My state doesn't work

Recommended Posts

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
[CODE]
#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;
}
[/CODE]

here is my gState.h
[CODE]
//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
[/CODE]

and my gState.cpp
[CODE]
#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() );
}
}
}
[/CODE]


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

Share this post


Link to post
Share on other sites
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?

[CODE]gButton newBtn(100,100,96,32);
buttons.push_back(newBtn);[/CODE]

[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++ Edited by Alpha_ProgDes

Share this post


Link to post
Share on other sites
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 [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img],
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 Edited by Canvas

Share this post


Link to post
Share on other sites
[CODE]
using namespace std;
int main(int argc, char* args[] )
{
gState gameState;
if( gameState.init() == false)
{
return 1;
}
gameState.run(1);
gameState.update();
return 0;
}[/CODE]

Try that and tell me how it works out.

Share this post


Link to post
Share on other sites
[quote name='Canvas' timestamp='1339274725' post='4947744']
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
[/quote]
Ahh, I see. That makes sense, lol. Well I relearned something new today. :) Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this