Hey. I have a button class. Something like this in pseudocode
But that still doesn’t work.
You don’t show the whole code … did you do an update
to copy the surface in RAM to the hardware window?
Button.h
Code:
#ifndef Button_Button_h
#define Button_Button_h
#include “Util.h”
class Button {
public:
SDL_Surface * image = NULL;
SDL_Surface * curr = NULL;
SDL_Rect clips[2];
SDL_Rect area;
void init(std::string, int, int, int, int);
void handleEvent(SDL_Event *);
SDL_Surface * getSurface();
SDL_Rect * getRect();
};
#endif
Button.mm
Code:
#include “Button.h”
void Button::init(std::string filename, int x, int y, int w, int h){
area.x = x;
area.y = y;
area.w = w;
area.h = h;
clips[0].x = 0;
clips[0].y = 0;
clips[0].w = w;
clips[0].h = h;
clips[1].x = w;
clips[1].y = 0;
clips[1].w = w;
clips[1].h = h;
image = loadImage(filename);
//applySurface(image, curr, 20, 20, &clips[0]);
curr = image;
};
SDL_Surface * Button::getSurface(){
return curr;
};
Util.h
Code:
#ifndef Button_Util_h
#define Button_Util_h
#include <Cocoa/Cocoa.h>
#include <Foundation/Foundation.h>
#include <SDL/SDL.h>
#include <SDL_image/SDL_image.h>
#include
SDL_Surface * loadImage(std::string);
void applySurface(SDL_Surface *, SDL_Surface *, int = 0, int = 0, SDL_Rect * = NULL);
#endif
Util.mm
Code:
#include “Util.h”
SDL_Surface * loadImage(std::string filename){
SDL_Surface * loadedImage = NULL;
SDL_Surface * optImage = NULL;
loadedImage = IMG_Load(filename.c_str());
if (loadedImage == NULL){ // If error occurs
return NULL;
};
optImage = SDL_DisplayFormatAlpha(loadedImage);
SDL_FreeSurface(loadedImage);
return optImage;
};
void applySurface(SDL_Surface * from, SDL_Surface * to, int x, int y, SDL_Rect * clip){
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(from, clip, to, &offset);
};
main.mm
Code:
#include “Util.h”
#include “Button.h”
class game {
public:
bool isRunning = false;
const int screenWidth = 640;
const int screenHeight = 480;
const int screenBPP = 32;
SDL_Surface * screen = NULL;
SDL_Event event;
/////////////////////////////////////
SDL_Surface * background = NULL;
Button button;
/////////////////////////////////////
int init();
int loadResources();
int run();
int render();
int cleanUp();
};
int game::init(){
if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
NSLog(@“Error”);
return 1;
};
screen = SDL_SetVideoMode(screenWidth, screenHeight, screenBPP, NULL);
if (screen == NULL){
NSLog(@"Error");
return 1;
};
loadResources();
return 0;
};
int game::loadResources(){
button.init(“Button.png”, 160, 120, 320, 240);
background = loadImage(“Background.jpg”);
return 0;
}
int game::run(){
isRunning = true;
while (isRunning){
render();
while (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT){
isRunning = false;
cleanUp();
};
};
};
return 0;
};
int game::render(){
applySurface(background, screen);
applySurface(button.getSurface(), screen);
SDL_Flip(screen);
return 0;
};
int game::cleanUp(){
SDL_Quit();
return 0;
};
int main(int argc, char * argv[]){
game g;
g.init();
g.run();
return 0;
};
Sorry, I’m pretty new to SDL.
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org