Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    570
  • comments
    2427
  • views
    216941

Entry #56.c

Sign in to follow this  
Mushu

84 views

CoDe DuMp!!
(this is like free webhosting for me)

This, pretty much is a SDL sprite class I'm working on. It will be contained within a parent "object" class for some game engine (no ideas yet) that I will implement. Also coming soon, an SDL Event class...

Feel free to use this if it suits you. Hopefully I'll have more time to work on it tommorrow.

// the header

#ifndef GUARD_SPRITE
#define GUARD_SPRITE

#include "sdl.h"
#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*);

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

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 setDest(SDL_Surface*);

void setSrc(const char*);
void setSrc(SDL_Surface*);

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

void move(int x, int y);

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

private:

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

};

#endif

// the implementation (a mess)
#include "sprite.h"

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() {
SDL_BlitSurface(src, &rect, dest, &loc);
}

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

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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!