// statemachine.h
#ifndef STATEMACHINE_H
#define STATEMACHINE_H
#include <vector>
#include "SDL/SDL.h"
#include "screen.h"
class SFrameState
{
protected:
SFrameScreen screen;
public:
SFrameState(){}
SFrameState(SFrameScreen _screen);
SFrameState(const SFrameState &rhs) {}
const SFrameState operator=(const SFrameState &rhs);
virtual void handleInput(const SDL_Event event);
virtual void render();
virtual void logic();
};
class SFrameStateMachine
{
private:
SFrameScreen screen;
std::vector<SFrameState> states;
public:
SFrameStateMachine(SFrameScreen &_screen, std::vector<SFrameState> _states);
};
#endif
//statemachine.cpp
#include "statemachine.h"
SFrameStateMachine::SFrameStateMachine(SFrameScreen &_screen, std::vector<SFrameState> _states)
{
screen = _screen;
states = _states;
}
const SFrameState SFrameState::operator =(const SFrameState &rhs)
{
return *this;
}
The machine will take in a vector of states and use this to switch between them, here's my test program:
#include <iostream>
#include "sdlframe.h"
class StateMain : SFrameState
{
private:
SFrameTimer timer;
SFrameImage image;
int x,speed;
public:
StateMain();
void handleInput(SDL_Event event);
void render();
void logic();
};
StateMain::StateMain()
{
x = 0;
speed = 100;
// image("dot.png");
image = SFrameImage("dot.png");
}
void StateMain::handleInput(SDL_Event event)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
}
}
}
void StateMain::render()
{
screen.fill(255,255,255);
screen.blit(x,120,image,NULL);
}
void StateMain::logic()
{
float distance = timer.tick()/1000000.;
distance *= speed;
x += distance;
}
int main()
{
SFrameInitEverything();
SFrameScreen screen(640,480,32);
SFrameImage image("dot.bmp");
SDL_Event event;
SFrameTimer t;
float x = 0;
int speed = 100;
bool quit = false;
while(quit == false)
{
float distance = t.tick()/1000000.;
distance *= speed;
x += distance;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
quit = true;
}
}
screen.fill(255,255,255);
screen.blit(x,120,image,NULL);
if(screen.flip() == false)
{
quit = true;
}
std::cout << distance << std::endl;
std::cout << x << std::endl;
}
SFrameCloseEverything();
return 1;
}
When I compile this the compiler outputs:
/home/matthew/Documents/Programming/C++/SDL/framework/demo/test.cpp:22: error: no match for ‘operator=’ in ‘((StateMain*)this)->StateMain::image = SFrameImage(std::basic_string<char, std::char_traits<char>, std::allocator<char> >(((const char*)"dot.png"), ((const std::allocator<char>&)((const std::allocator<char>*)(& std::allocator<char>())))))’
Why is this? I think it is something to do with the way I defined operator new:
// Image class inherits Surface
const SFrameSurface SFrameSurface::operator =(SFrameSurface &rhs)
{
width = rhs.width;
height = rhs.height;
surface = rhs.surface;
return *this;
}
const SFrameImage& SFrameImage::operator =(std::string filename)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}else{
throw std::string("Image could not be loaded (does is exist?)");
}
surface = optimizedImage;
return *this;
}
void *SFrameImage::operator new(size_t size)
{
void *p;
p = malloc(size);
return p;
}
void SFrameImage::operator delete(void *p)
{
free(p);
}
Sorry for this badly written post!


Sign In
Create Account



Back to top









