• Advertisement
Sign in to follow this  
  • entries
    570
  • comments
    2427
  • views
    216616

Entry #57.c

Sign in to follow this  

89 views

Yeah, another code dump. I'm working on crap in two different places, probably won't be able to access this one, FTP is blocked, yadda yadda, this is my journal to spam up anyway so bitch at me all you want because this is a public spamming area.

Anyway, this is the (almost) finished sprite class. It's only missing SDL_image support (for formats other than *.bmp) and per-pixel rotations. Actually, there are no image rotations yet because per-pixel (IRC) is the only way to do it in SDL.

THE CODE!

// SDL Sprite class

// still haven't done per-pixel rotations, but everything else is
// implemented. Guess I'll have to live without rotations unless I
// feel like I really need them. I'll probably have to write some
// function eventually to flip the image, either horizontally or
// vertically.

// oh - and I need to add SDL_image support so that it will take
// image formats other than *.bmp. Heh. Oops.

// anyway, on with the show.
// ~Mushu.

// PS: if anyone actually decides they want to use this, and find
// an error or other fuckup somewhere, PM me on GDNET so I can fix
// it if I haven't already found it. Thanks! ;)

// header
#ifndef GUARD_SPRITE
#define GUARD_SPRITE

#include "sdl.h"
#include
#include
#include

SDL_Rect mRect(int, int, int, int);

class sprite {
// this is the class that should provide all
// of our 2D needs. Hopefully.

public:
sprite();
sprite(const char*);
sprite(SDL_Surface*);
sprite(const char*, SDL_Surface*);
~sprite();

void lock();
void unlock();

void setPixel(int,int,Uint32);
void setPixel(int,int,int,int,int);

const char* getPixel(int,int);

void convertSurface();
void convertSurface(SDL_Surface*);

void setColorKey(Uint32);
void setColorKey(int,int,int);

void setSurfaceAlpha(Uint8);

void setLocR(SDL_Rect);
void setLocR(int,int);
void setLocR(int,int,int,int);

void setSrcR();
void setSrcR(SDL_Rect);
void setSrcR(int,int);
void setSrcR(int,int,int,int);

void render();
void render(SDL_Surface*);
void render(SDL_Rect*);
void render(int, int, int, int);
void render(int, int);

void setDest(SDL_Surface*);

void setSrc(const char*);
void setSrc(SDL_Surface*);
void setSrc(Uint32, int, int, unsigned int, Uint32, Uint32, Uint32, Uint32);
void setSrc(int, int, unsigned int);

void fillRect(Uint32);
void fillRect(int, int, int);

void move(int, int);

SDL_Surface* getSurface();
SDL_Surface* getDestSurface();
SDL_Rect getRect();
SDL_Rect getLoc();
SDL_PixelFormat* getFormat();

private:

bool locked;
SDL_Surface* src;
SDL_Surface* dest;
SDL_Rect rect;
SDL_Rect loc;

};

#endif

// implementation
#include "sprite.h"

const char* sprite::getPixel(int x, int y) {
if (!src)
return NULL;

if (!locked)
lock();

char* pData;

pData = (char*)src->pixels;
pData += (y * src->pitch);
pData += (x * src->format->BytesPerPixel);

return pData;
}

void sprite::setPixel(int x, int y, int r, int g, int b) {
if (!src)
return;

setPixel(x,y, SDL_MapRGB(src->format, r, g, b));
}

void sprite::setPixel(int x, int y, Uint32 color) {
if (!src)
return;

if (!locked)
lock();

char* pData;

pData = (char*)src->pixels;
pData += (y * src->pitch);
pData += (x * src->format->BytesPerPixel);

memcpy(pData, &color, src->format->BytesPerPixel);
}

void sprite::unlock() {
if (SDL_MUSTLOCK(src))
SDL_UnlockSurface(src);

locked = false;
}

void sprite::lock() {
if (SDL_MUSTLOCK(src))
SDL_LockSurface(src);

locked = true;
}

void sprite::convertSurface() {
if (dest)
convertSurface(dest);
}

void sprite::convertSurface(SDL_Surface* fmtSurf) {
// duplicate the src pointer
SDL_Surface* oldSrc = src;

// convert surface
src = SDL_ConvertSurface(src, fmtSurf->format, src->flags); //retain old flags

// deallocate old surface from memory
SDL_FreeSurface(oldSrc);
}

sprite::~sprite() {
SDL_FreeSurface(src);
dest = NULL; // DO NOT DELETE THIS; may be used by other objects

// deleting the pointers should deallocate the memory.

//this doesn't work because it calls a user-defined breakpoint
//which we really don't want. This will result in a _slight_
//memory leak, which _hopefully_ we won't have to worry about.

//delete ▭
//delete &loc;
}

void sprite::setSrc(Uint32 flags, int w, int h, unsigned int d, Uint32 r, Uint32 g, Uint32 b, Uint32 a) {
if (r + b + g + a != d)
d = r + b + g + a;

src = SDL_CreateRGBSurface(flags, w, h, d, r, g, b, a);
}

void sprite::setSrc(int w, int h, unsigned int d = 24) {
setSrc(SDL_HWSURFACE, w, h, 24, 6, 6, 6, 6);
}

void sprite::setSurfaceAlpha(Uint8 alpha) {
SDL_SetAlpha(src, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
}

void sprite::move(int x, int y) {
loc.x += x;
loc.y += y;
}

SDL_PixelFormat* sprite::getFormat() {
return src->format;
}

void sprite::setColorKey(Uint32 colorkey) {
if (src)
SDL_SetColorKey(src, SDL_SRCCOLORKEY, colorkey);
}

void sprite::setColorKey(int r, int g, int b) {
if (src)
setColorKey(SDL_MapRGB(src->format, r, g, b));
}

void sprite::setSrc(SDL_Surface* newSrc) {
src = NULL;

if (newSrc)
src = newSrc;
}

void sprite::setSrc(const char* srcfile) {
src = SDL_LoadBMP(srcfile);
}

SDL_Surface* sprite::getSurface() {
return src;
}

SDL_Surface* sprite::getDestSurface() {
return dest;
}

SDL_Rect sprite::getLoc() {
return loc;
}

SDL_Rect sprite::getRect() {
return rect;
}

void sprite::fillRect(int r, int g, int b) {
fillRect(SDL_MapRGB(dest->format, r, g, b));
}

void sprite::fillRect(Uint32 color) {
SDL_FillRect(dest, &loc, color);
}

void sprite::setSrcR() {
rect = mRect(0,0,src->w, src->h);
}

void sprite::setLocR(SDL_Rect newRect) {
loc = newRect;
}

void sprite::setSrcR(SDL_Rect newRect) {
rect = newRect;
}

void sprite::setDest(SDL_Surface* newDest) {
if (!newDest) {
fprintf(stderr, "NULL Pointer passed to load sprite.");
return;
}

dest = newDest;
}

void sprite::setSrcR(int x, int y, int w, int h) {
rect = mRect(x,y,w,h);
}

void sprite::setSrcR(int x, int y) {
rect.x = x;
rect.y = y;
}

void sprite::setLocR(int x, int y, int w, int h) {
loc = mRect(x,y,w,h);
}

void sprite::setLocR(int x, int y) {
loc.x = x;
loc.y = y;
}

sprite::sprite() : src(NULL), dest(NULL) {}

sprite::sprite(const char* srcfile) {
if (src)
SDL_FreeSurface(src);

src = NULL;
src = SDL_LoadBMP(srcfile);

if (src == NULL) {
fprintf(stderr, "\nBitmap failed to load from sourcefile.");
return;
}
}

sprite::sprite(SDL_Surface* srcSurface) {
if (!srcSurface) {
fprintf(stderr, "\nNULL surface pointer passed to create sprite.");
return;
}

if (src)
SDL_FreeSurface(src);

src = NULL;
src = srcSurface;
}

sprite::sprite(const char* srcfile, SDL_Surface* newDest) {
sprite::sprite();

if (srcfile == NULL) {
fprintf(stderr, "\nNULL string passed as argument to create sprite.");
return;
}

if (!newDest) {
fprintf(stderr, "\nNULL surface pointer passed to create sprite.");
return;
}

setSrc(srcfile);
dest = newDest;

}

void sprite::render(SDL_Rect* ploc) {
SDL_BlitSurface(src, &rect, dest, ploc);
}

void sprite::render() {
if (locked) {
fprintf(stderr, "\nSprite not unlocked!! Doing automatically!");
unlock();
}

SDL_BlitSurface(src, &rect, dest, &loc);
}

void sprite::render(SDL_Surface* newDest) {
dest = newDest;
render();
}

void sprite::render(int x, int y, int w, int h) {
SDL_Rect tmpRect = mRect(x, y, w, h);
render(&tmpRect);
}

void sprite::render(int x, int y) {
render(x, y, rect.w, rect.h);
}

SDL_Rect mRect(int x, int y, int w, int h) {
SDL_Rect ret;

ret.x = x;
ret.y = y;
ret.w = w;
ret.h = h;

return ret;
}
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

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

  • Advertisement