Blit to surface with clip, then blit that surface to screen

Hey. I have a button class. Something like this in pseudocode

Button

Code:

import util

class button {
surface *image = NULL
surface *currentSurface = NULL
rect clip[2];

void init (graphicaName, x, y, w, h){
     image = open(graphicName) // think a sprite-sheet of buttons
     (set clip values based on w, and h)
     util.applySurface(image, curr, 0, 0, &clips[0])
}

surface * getSurface(){
    return currentSurface
}

}

util

Code:

void applySurface(from, to, x, y, clip){
rect offset;
(set offset values based on x and y)
SDL_BlitSurface(from, clip, to, &offset)
}

Now, the idea is that when button is opened the current image state of that button (whether it be up or down) is stored in the button. Events are then handled in the button (this changes the state)

When you need to show the button, you call getSurface which will return the current surface of the button so you can blit it to the surface.

Now, when I do this - nothing happens (it shows no button). However, setting currentSurface = image shows the button (albeit it shows the whole spritesheet instead of just that state) so I know the loading function isn’t broken.

Suggestions? Am I doing this wrong?

I’ve tried creating multiple surfaces somewhat like this

Code:

surface * off = NULL
surface * on = NULL
off = image
on = image

off.setClipRect(rect)
off.setClipRect(differentRect)

But that still doesn’t work.

P.S. is there a more efficient method than what I am currently doing?

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?On 22/08/2013, at 9:11 AM, Plazmotech wrote:


john skaller
@john_skaller
http://felix-lang.org

john skaller wrote:> On 22/08/2013, at 9:11 AM, Plazmotech wrote:

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