Mudei o remake original para a directoria original

Adicionei o novo cppcafe que tenho tado a trabalhar as fazes no Visual Studio
É baseado numa classe que achei na net para Sprites em SDL
This commit is contained in:
Pedro de Oliveira 2010-02-04 21:34:00 +00:00
parent 30ee8f47f9
commit bc1fedfb0f
33 changed files with 1399 additions and 0 deletions

BIN
cppcafe/cppcafe.ncb Normal file

Binary file not shown.

20
cppcafe/cppcafe.sln Normal file
View File

@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cppcafe", "cppcafe\cppcafe.vcproj", "{20D8BAE2-2505-44AE-AA1A-C9A2A521BA48}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{20D8BAE2-2505-44AE-AA1A-C9A2A521BA48}.Debug|Win32.ActiveCfg = Debug|Win32
{20D8BAE2-2505-44AE-AA1A-C9A2A521BA48}.Debug|Win32.Build.0 = Debug|Win32
{20D8BAE2-2505-44AE-AA1A-C9A2A521BA48}.Release|Win32.ActiveCfg = Release|Win32
{20D8BAE2-2505-44AE-AA1A-C9A2A521BA48}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

BIN
cppcafe/cppcafe.suo Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel>
</requestedPrivileges>
</security>
</trustInfo>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.VC90.DebugCRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.VC80.CRT" version="8.0.50727.762" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
</dependentAssembly>
</dependency>
</assembly>

Binary file not shown.

View File

@ -0,0 +1,20 @@
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level='asInvoker' uiAccess='false' />
</requestedPrivileges>
</security>
</trustInfo>
<dependency>
<dependentAssembly>
<assemblyIdentity type='win32' name='Microsoft.VC90.DebugCRT' version='9.0.21022.8' processorArchitecture='x86' publicKeyToken='1fc8b3b9a1e18e3b' />
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity type='win32' name='Microsoft.VC80.CRT' version='8.0.50727.762' processorArchitecture='x86' publicKeyToken='1fc8b3b9a1e18e3b' />
</dependentAssembly>
</dependency>
</assembly>

Binary file not shown.

View File

@ -0,0 +1 @@
Manifest resource last updated at 9:34:46,36 on 19-01-2010

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,20 @@
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level='asInvoker' uiAccess='false' />
</requestedPrivileges>
</security>
</trustInfo>
<dependency>
<dependentAssembly>
<assemblyIdentity type='win32' name='Microsoft.VC90.CRT' version='9.0.21022.8' processorArchitecture='x86' publicKeyToken='1fc8b3b9a1e18e3b' />
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity type='win32' name='Microsoft.VC80.CRT' version='8.0.50727.762' processorArchitecture='x86' publicKeyToken='1fc8b3b9a1e18e3b' />
</dependentAssembly>
</dependency>
</assembly>

Binary file not shown.

View File

@ -0,0 +1 @@
Manifest resource last updated at 9:35:08,33 on 19-01-2010

Binary file not shown.

Binary file not shown.

737
cppcafe/cppcafe/Sprite.cpp Normal file
View File

@ -0,0 +1,737 @@
#include "Sprite.h"
using namespace std;
/*
Sprite library for SDL - using bitmaps
This library was put together by Kenny Cason and is designed to
be easily implemented into any C++ program using SDL
Feel free to do what ever you want with it. enjoy!
Please report any bugs
kenneth [DOT] cason [AT] gmail [DOT] com
v1.0
2009 Sep 20
www.ken-soft.com
*/
Sprite::Sprite() {
sprite = NULL;
loaded = false;
width = 0;
height = 0;
speed = 0;
run = false;
loopToBeginning = true;
indexIterator = 0;
index = 0;
}
Sprite::Sprite(string file, int frames, int speed) {
SDL_Surface *temp = SDL_LoadBMP(file.c_str());
sprite = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
if(sprite == NULL) {
cout << "failed to load sprite " << file << endl;
loaded = false;
width = 0;
height = 0;
this->speed = 0;
} else {
cout << "successfully loaded sprite " << file << endl;
loaded = true;
width = sprite->w/frames;
height = sprite->h;
this->speed = speed;
run = true;
}
maxFrames = frames;
if(maxFrames > 1) {
run = true;
} else {
run = false;
}
lastAnimated = SDL_GetTicks();
index = 0;
indexIterator = 1;
loopToBeginning = true;
}
Sprite::Sprite(SDL_Surface* surface, int frames, int speed) {
if(surface == NULL) {
cout << "failed to load sprite" << endl;
sprite = NULL;
loaded = false;
width = 0; height = 0;
this->speed = 0;
} else {
cout << "successfully loaded sprite" << endl;
// create a new surface
if(surface->flags & SDL_SRCCOLORKEY) {
sprite = SDL_CreateRGBSurfaceFrom(surface->pixels, surface->w, surface->h, surface->format->BitsPerPixel, surface->pitch,
surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, 0 );
} else {
sprite = SDL_CreateRGBSurfaceFrom(surface->pixels, surface->w, surface->h, surface->format->BitsPerPixel, surface->pitch,
surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask );
}
if(surface->flags & SDL_SRCCOLORKEY) {
SDL_SetColorKey(sprite, SDL_RLEACCEL|SDL_SRCCOLORKEY, surface->format->colorkey );
}
loaded = true;
width = sprite->w/frames;
height = sprite->h;
this->speed = speed;
}
maxFrames = frames;
if(maxFrames > 1) {
run = true;
} else {
run = false;
}
lastAnimated = SDL_GetTicks();
indexIterator = 1;
index = 0;
loopToBeginning = true;
}
void Sprite::draw(SDL_Surface* buffer, int x, int y) {
if(!isSprite()) {
cout << "Failed to draw, Sprite not initialized!"<< endl;
return;
}
SDL_Rect dstrect;
dstrect.x = x;
dstrect.y = y;
// this blits the current frame from the sprite sheet
SDL_Rect animRect;
animRect.x = width*index;
animRect.y = 0;
animRect.w = width;
animRect.h = height;
SDL_BlitSurface(sprite, &animRect, buffer,&dstrect);
}
Sprite::~Sprite() {
SDL_FreeSurface(sprite);
}
void Sprite::setTransparency(int r, int g, int b) {
if(!isSprite()) {
cout << "Failed to set Transparency, Sprite not initialized!"<< endl;
return;
}
SDL_SetColorKey(sprite, SDL_SRCCOLORKEY,SDL_MapRGB(sprite->format, r, g, b));
}
void Sprite::setTransparency(Uint32 colorkey) {
if(!isSprite()) {
cout << "Failed to set Transparency, Sprite not initialized!"<< endl;
return;
}
SDL_SetColorKey(sprite, SDL_SRCCOLORKEY, colorkey);
}
void Sprite::setSpeed(int i) {
speed = i;
}
void Sprite::start() {
run = true;
}
void Sprite::restart() {
if(run) {
index = 0;
lastAnimated = SDL_GetTicks();
}
}
void Sprite::animate() {
if(run) {
if(SDL_GetTicks() - lastAnimated > speed) {
lastAnimated = SDL_GetTicks();
index += indexIterator;
if(index >= maxFrames) {
if(loopToBeginning) {
indexIterator = 1;
index = 0;
} else {
indexIterator = -1;
index = maxFrames - 1;
}
} else if(index < 0) {
indexIterator = 1;
index = 0;
}
}
}
}
void Sprite::setLoopToBegin(bool loop) {
loopToBeginning = loop;
}
bool Sprite::running() {
return run;
}
void Sprite::stop() {
run = false;
index = 0;
}
bool Sprite::isSprite() {
return loaded;
}
void Sprite::setTransparentPixel(int x, int y) {
setPixel(x, y, sprite->format->colorkey);
}
bool Sprite::isTransparentPixel(int x, int y) {
Uint32 pixelcolor = getPixel(x, y);
//test whether pixels color == color of transparent pixels for that surface
return (pixelcolor == sprite->format->colorkey);
}
void Sprite::setPixel(int x, int y, int r, int g, int b) {
Uint32 color;
color += b*256*256;
color += g*256;
color += r;
setPixel(x,y,color);
}
void Sprite::setPixel(int x, int y, Uint32 pixel) {
setPixel(sprite, x, y, pixel);
}
Uint32 Sprite::getPixel(int x, int y) {
return getPixel(sprite, x, y);
}
Uint8 Sprite::getPixel8(int x, int y) {
if(!isSprite()) {
cout << "Failed to get pixel, Sprite not initialized!"<< endl;
return -1;
}
Uint8* pixels = (Uint8*)sprite->pixels;
return pixels[y * sprite->w + x];
}
void Sprite::setPixel8(int x, int y, Uint8 pixel) {
if(!isSprite()) {
cout << "Failed to set pixel, Sprite not initialized!"<< endl;
return;
}
Uint8* pixels = (Uint8*)sprite->pixels;
pixels[y * sprite->w + x] = pixel;
}
Uint16 Sprite::getPixel16(int x, int y) {
if(!isSprite()) {
cout << "Failed to get pixel, Sprite not initialized!"<< endl;
return -1;
}
Uint16* pixels = (Uint16*)sprite->pixels;
return pixels[y * sprite->w + x];
}
void Sprite::setPixel16(int x, int y, Uint16 pixel) {
if(!isSprite()) {
cout << "Failed to set pixel, Sprite not initialized!"<< endl;
return;
}
Uint16* pixels = (Uint16*)sprite->pixels;
pixels[y * sprite->w + x] = pixel;
}
Uint32 Sprite::getPixel32(int x, int y) {
if(!isSprite()) {
cout << "Failed to get pixel, Sprite not initialized!"<< endl;
return -1;
}
Uint32* pixels = (Uint32*)sprite->pixels;
return pixels[y * sprite->w + x];
}
void Sprite::setPixel32(int x, int y, Uint32 pixel) {
if(!isSprite()) {
cout << "Failed to set pixel, Sprite not initialized!"<< endl;
return;
}
Uint32* pixels = (Uint32*)sprite->pixels;
pixels[y * sprite->w + x] = pixel;
}
int Sprite::getFrame() {
return index;
}
int Sprite::getFrameWidth() { // width of each frame
return width;
}
int Sprite::getFrameHeight() { // height of each frame, in this implementation it is the sprites actual height
return height;
}
int Sprite::getWidth() { // this is most likely the width a user will need to know
return getFrameWidth();
}
int Sprite::getSpriteWidth() { // sprites Actual width
return sprite->w;
}
int Sprite::getHeight() { // in this Class the frame height is the same as the sprites Actual height
return sprite->h;
}
int Sprite::getSpriteHeight() { // sprites Actual height
return sprite->h;
}
bool Sprite::equals(Sprite cmp) {
if(sprite == cmp.getSurface()) {
return true;
}
return false;
}
SDL_Surface* Sprite::getSurface() {
return sprite;
}
void Sprite::setSurface(SDL_Surface* surface) {
sprite = surface;
}
SDL_Surface* Sprite::getRect(int x, int y, int width, int height) {
return getRect(*this, x, y, width, height);
}
bool Sprite::rectCollide(int x1, int y1, Sprite &spriteB, int x2, int y2) {
return rectCollide(*this, x1, y1, spriteB, x2, y2);
}
bool Sprite::pixelCollide(int x1, int y1, Sprite &spriteB, int x2, int y2) {
return pixelCollide(*this, x1, y1, spriteB, x2, y2);
}
void Sprite::rotate90() {
rotate90(*this);
}
void Sprite::rotate180() {
rotate180(*this);
}
void Sprite::rotate270() {
rotate270(*this);
}
void Sprite::flipHorizontal() {
flipHorizontal(*this);
}
void Sprite::flipVertical() {
flipVertical(*this);
}
void Sprite::fade(float f) {
fade(*this, f);
}
void Sprite::reverseAnimation() {
reverseAnimation(*this);
}
void Sprite::zoom(float z) {
zoom(*this, z);
}
void Sprite::stretchX(float x) {
stretchX(*this, x);
}
void Sprite::stretchY(float y) {
stretchY(*this, y);
}
void Sprite::stretch(float x,float y) {
stretch(*this, x, y);
}
void Sprite::destroy() {
SDL_FreeSurface(sprite);
}
/*
***********************************************************
Static functions to cut down on memory usage
*********************************************************** */
SDL_Surface* Sprite::getRect(Sprite &sprite, int x, int y, int w, int h) {
if(!sprite.isSprite()) {
cout << "Failed to get Rectangle, Sprite not initialized!"<< endl;
return NULL;
}
SDL_Surface* newrect;
// create a new surface
if(sprite.getSurface()->flags & SDL_SRCCOLORKEY) {
newrect = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, sprite.getSurface()->format->BitsPerPixel,
sprite.getSurface()->format->Rmask, sprite.getSurface()->format->Gmask, sprite.getSurface()->format->Bmask, sprite.getSurface()->format->Amask );
} else {
newrect = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, sprite.getSurface()->format->BitsPerPixel,
sprite.getSurface()->format->Rmask, sprite.getSurface()->format->Gmask, sprite.getSurface()->format->Bmask, 0 );
}
for(int j = 0; j < h; j++) {
for(int i = 0; i < w; i++) {
setPixel(newrect, i, j, sprite.getPixel(x+i,y+j));
}
}
//Copy color key
if(sprite.getSurface()->flags & SDL_SRCCOLORKEY) {
SDL_SetColorKey(newrect, SDL_RLEACCEL|SDL_SRCCOLORKEY, sprite.getSurface()->format->colorkey );
}
return newrect;
}
/*
test
testX,testY____ testX+width
| |
| sprite |
| thisX,thisY |__ thisX+width
| | | |
|__|___________| |
testY+height |
|_____________|
thisY+height
*/
bool Sprite::rectCollide(Sprite &spriteA, int aX, int aY, Sprite &spriteB, int bX, int bY) {
if(!spriteA.isSprite() || !spriteB.isSprite()) {
cout << "Failed to perfrom Rectangle Collision test, Sprite not initialized!"<< endl;
return false;
}
if((aX + spriteA.getWidth() < bX) || (bX + spriteB.getWidth() < aX)) {
return false;
}
if((aY + spriteA.getHeight() < bY) || (bY + spriteB.getHeight() < aY)) {
return false;
}
return true;
}
bool Sprite::pixelCollide(Sprite &spriteA, int aX, int aY, Sprite &spriteB, int bX, int bY) {
/*check if bounding boxes intersect*/
if(!rectCollide(spriteA, aX, aY, spriteB, bX, bY)) {
return false;
}
// get the overlaping box
int inter_x0 = SPRITE_MAX(bX,aX);
int inter_x1 = SPRITE_MIN(bX+spriteB.getWidth(),aX+spriteA.getWidth());
int inter_y0 = SPRITE_MAX(bY,aY);
int inter_y1 = SPRITE_MIN(bY+spriteB.getHeight(),aY+spriteA.getHeight());
for(int y = inter_y0 ; y <= inter_y1 ; y++) {
for(int x = inter_x0 ; x <= inter_x1 ; x++) {
/*compute offsets for surface, but dont forget to account for the current animation*/
if((!spriteB.isTransparentPixel(x-aX + spriteB.index * spriteB.getWidth() , y-bY))
&& (!spriteA.isTransparentPixel(x-aX + spriteA.index * spriteA.getWidth(), y-aY))) {/*before pass to isTransparentPixel*/
return true;
}
}
}
return false;
}
void Sprite::flip(Sprite &sprite, int val) {
if(!sprite.isSprite()) {
cout << "Failed to flip, Sprite not initialized!"<< endl;
return;
}
SDL_Surface* flipped;
// create a new surface
if(sprite.getSurface()->flags & SDL_SRCCOLORKEY) {
flipped = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite.getSurface()->w, sprite.getSurface()->h, sprite.getSurface()->format->BitsPerPixel,
sprite.getSurface()->format->Rmask, sprite.getSurface()->format->Gmask, sprite.getSurface()->format->Bmask, sprite.getSurface()->format->Amask );
} else {
flipped = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite.getSurface()->w, sprite.getSurface()->h, sprite.getSurface()->format->BitsPerPixel,
sprite.getSurface()->format->Rmask, sprite.getSurface()->format->Gmask, sprite.getSurface()->format->Bmask, 0 );
}
// check to see if the surface must be locked
if(SDL_MUSTLOCK(sprite.getSurface())) {
SDL_LockSurface(sprite.getSurface());
}
for(int y = 0; y < sprite.getSurface()->h; y++) {
for(int x = 0; x < sprite.getSurface()->w; x++) {
if(val == FLIP_HORIZONTAL) {
setPixel(flipped, sprite.getSurface()->w - 1 - x, y, sprite.getPixel(x,y));
} else if(val == FLIP_VERTICAL) {
setPixel(flipped, x, sprite.getSurface()->h - 1 - y, sprite.getPixel(x,y));
}
}
}
//Copy color key
if(sprite.getSurface()->flags & SDL_SRCCOLORKEY) {
SDL_SetColorKey(flipped, SDL_RLEACCEL|SDL_SRCCOLORKEY, sprite.getSurface()->format->colorkey );
}
if(SDL_MUSTLOCK(sprite.getSurface())) {
SDL_UnlockSurface(sprite.getSurface());
}
SDL_FreeSurface(sprite.getSurface());
sprite.setSurface(flipped);
}
void Sprite::flipVertical(Sprite &sprite) {
return flip(sprite, FLIP_VERTICAL);
}
void Sprite::flipHorizontal(Sprite &sprite) {
return flip(sprite, FLIP_HORIZONTAL);
}
void Sprite::rotate90(Sprite &sprite) {
rotate(sprite, 90);
}
void Sprite::rotate180(Sprite &sprite) {
rotate(sprite, 180);
}
void Sprite::rotate270(Sprite &sprite) {
rotate(sprite, 270);
}
void Sprite::rotate(Sprite &sprite, int deg) {
if(!sprite.isSprite()) {
cout << "Failed to rotate animation, Sprite not initialized!"<< endl;
return;
}
SDL_Surface* rotated;
int w,h;
if(deg == 90 || deg == 270) {
w = sprite.getHeight() * sprite.maxFrames;
h = sprite.getWidth();
} else if(deg == 180) {
w = sprite.getWidth() * sprite.maxFrames;
h = sprite.getHeight();
} else {
return;
}
// create a new surface
if(sprite.getSurface()->flags & SDL_SRCCOLORKEY) {
rotated = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, sprite.getSurface()->format->BitsPerPixel,
sprite.getSurface()->format->Rmask, sprite.getSurface()->format->Gmask, sprite.getSurface()->format->Bmask, sprite.getSurface()->format->Amask );
} else {
rotated = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, sprite.getSurface()->format->BitsPerPixel,
sprite.getSurface()->format->Rmask, sprite.getSurface()->format->Gmask, sprite.getSurface()->format->Bmask, 0 );
}
if(SDL_MUSTLOCK(sprite.getSurface())) {
SDL_LockSurface(sprite.getSurface());
}
for(int i = 0; i < sprite.maxFrames; i++) {
for(int y = 0; y < sprite.getHeight(); y++) {
for(int x = 0; x < sprite.getWidth(); x++) {
if(deg == 90) {
setPixel(rotated, sprite.getHeight() * i + sprite.getHeight() - y - 1, x, sprite.getPixel((i * sprite.getWidth()) + x,y));
} else if(deg == 180) {
setPixel(rotated, sprite.getWidth() * i + sprite.getWidth() - x - 1, rotated->h - y - 1, sprite.getPixel((i * sprite.getWidth()) + x,y));
} else if(deg == 270) {
setPixel(rotated, sprite.getHeight() * i + y, rotated->h - x - 1, sprite.getPixel((i * sprite.getWidth()) + x,y));
} else {
return;
}
}
}
}
sprite.width = rotated->w/sprite.maxFrames;
sprite.height = rotated->h;
if(sprite.getSurface()->flags & SDL_SRCCOLORKEY) {
SDL_SetColorKey(rotated, SDL_RLEACCEL|SDL_SRCCOLORKEY, sprite.getSurface()->format->colorkey );
}
if(SDL_MUSTLOCK(sprite.getSurface())) {
SDL_UnlockSurface(sprite.getSurface());
}
SDL_FreeSurface(sprite.getSurface());
sprite.setSurface(rotated);
}
void Sprite::fade(Sprite &sprite, float fade) {
return;
}
void Sprite::reverseAnimation(Sprite &sprite) {
if(!sprite.isSprite()) {
cout << "Failed to reverse animation, Sprite not initialized!" << endl;
return;
}
SDL_Surface* reversed = NULL;
// create a new surface
if(sprite.getSurface()->flags & SDL_SRCCOLORKEY) {
reversed = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite.getSurface()->w, sprite.getSurface()->h, sprite.getSurface()->format->BitsPerPixel,
sprite.getSurface()->format->Rmask, sprite.getSurface()->format->Gmask, sprite.getSurface()->format->Bmask, sprite.getSurface()->format->Amask );
} else {
reversed = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite.getSurface()->w, sprite.getSurface()->h, sprite.getSurface()->format->BitsPerPixel,
sprite.getSurface()->format->Rmask, sprite.getSurface()->format->Gmask, sprite.getSurface()->format->Bmask, 0);
}
// check to see if the surface must be locked
if(SDL_MUSTLOCK(sprite.getSurface())) {
SDL_LockSurface(sprite.getSurface());
}
for(int f = 0; f < sprite.maxFrames; f++) {
for(int y = 0; y < sprite.getSurface()->h; y++) {
for(int x = 0; x < sprite.getWidth(); x++) {
setPixel(reversed, (sprite.maxFrames - f - 1)*sprite.getWidth() + x , y, sprite.getPixel(f*sprite.getWidth()+x,y));
}
}
}
//Copy color key
if(sprite.getSurface()->flags & SDL_SRCCOLORKEY) {
SDL_SetColorKey(reversed, SDL_RLEACCEL|SDL_SRCCOLORKEY, sprite.getSurface()->format->colorkey );
}
if(SDL_MUSTLOCK(sprite.getSurface())) {
SDL_UnlockSurface(sprite.getSurface());
}
SDL_FreeSurface(sprite.getSurface());
sprite.setSurface(reversed);
}
void Sprite::stretchX(Sprite &sprite, float stretchX) {
return stretch(sprite, stretchX, 100);
}
void Sprite::stretchY(Sprite &sprite, float stretchY) {
return stretch(sprite, 100, stretchY);
}
void Sprite::zoom(Sprite &sprite, float zoom) {
return stretch(sprite, zoom,zoom);
}
void Sprite::stretch(Sprite &sprite, float stretchX, float stretchY) {
if(!sprite.isSprite()) {
cout << "Failed to zoom, Sprite not initialized!"<< endl;
return;
}
SDL_Surface* zoomed;
if(stretchX < 1 || stretchY < 1) {
cout << "Failed to zoom, value must be greater than zero!"<< endl;
return;
}
stretchX /= 100;
stretchY /= 100;
// create a new surface
if(sprite.getSurface()->flags & SDL_SRCCOLORKEY) {
zoomed = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite.getSurface()->w*stretchX, sprite.getSurface()->h*stretchY, sprite.getSurface()->format->BitsPerPixel,
sprite.getSurface()->format->Rmask, sprite.getSurface()->format->Gmask, sprite.getSurface()->format->Bmask, sprite.getSurface()->format->Amask );
} else {
zoomed = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite.getSurface()->w*stretchX, sprite.getSurface()->h*stretchY, sprite.getSurface()->format->BitsPerPixel,
sprite.getSurface()->format->Rmask, sprite.getSurface()->format->Gmask, sprite.getSurface()->format->Bmask, 0);
}
if(SDL_MUSTLOCK(sprite.getSurface())) {
SDL_LockSurface(sprite.getSurface());
}
int zoomedWidth = zoomed->w/sprite.maxFrames;
for(int y = 0; y < zoomed->h; y++) {
for(int x = 0; x < zoomedWidth; x++) {
// iterate over each animation as opposed to the whole sprite, to ensure that each animation is resized properly
for(int i = 0; i < sprite.maxFrames; i++) {
setPixel(zoomed, (zoomedWidth * i) + x, y, sprite.getPixel((sprite.getWidth() * i) + (int)(x/stretchX),(int)(y/stretchY) ));
}
}
}
sprite.width = zoomedWidth;
sprite.height = zoomed->h;
if(sprite.getSurface()->flags & SDL_SRCCOLORKEY ) {
SDL_SetColorKey(zoomed, SDL_RLEACCEL|SDL_SRCCOLORKEY, sprite.getSurface()->format->colorkey );
}
if(SDL_MUSTLOCK(sprite.getSurface())) {
SDL_UnlockSurface(sprite.getSurface());
}
SDL_FreeSurface(sprite.getSurface());
sprite.setSurface(zoomed);
}
void Sprite::setPixel(SDL_Surface* sprite, int x, int y, Uint32 pixel) {
if(sprite == NULL) {
cout << "Failed to set pixel, Sprite not initialized!"<< endl;
return;
}
int bpp = sprite->format->BytesPerPixel;
/* p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)sprite->pixels + y * sprite->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
Uint32 Sprite::getPixel(SDL_Surface* sprite, int x, int y) {
if(sprite == NULL) {
cout << "Failed to get pixel, Sprite not initialized!"<< endl;
return -1;
}
int bpp = sprite->format->BytesPerPixel;
/* p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)sprite->pixels + y * sprite->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
return p[0] << 16 | p[1] << 8 | p[2];
} else {
return p[0] | p[1] << 8 | p[2] << 16;
}
break;
case 4:
return *(Uint32 *)p;
default:
return 0;
}
}

135
cppcafe/cppcafe/Sprite.h Normal file
View File

@ -0,0 +1,135 @@
#ifndef Sprite_h
#define Sprite_h
#include <cstdlib>
#include <iostream>
#include <string>
#include <math.h>
#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif
#include <SDL.h>
/*
Sprite library for SDL - using bitmaps
This library was put together by Kenny Cason and is designed to
be easily implemented into any C++ program using SDL
Feel free to do what ever you want with it. enjoy!
Please report any bugs
kenneth [DOT] cason [AT] gmail [DOT] com
v1.0
2009 Sep 20
www.ken-soft.com
*/
#define SPRITE_MAX(a,b) ((a > b) ? a : b)
#define SPRITE_MIN(a,b) ((a < b) ? a : b)
#define FLIP_HORIZONTAL 1
#define FLIP_VERTICAL 2
using namespace std;
class Sprite {
public:
Sprite();
~Sprite();
Sprite(string fileName,int maxFrames, int animationSpeed);
Sprite(SDL_Surface* surface, int maxFrames,int animationSpeed);
void draw(SDL_Surface* buffer, int x, int y);
void setTransparency(Uint32 color);
void setTransparency(int red, int green, int blue);
void setSpeed(int animationSpeed);
void start();
void restart();
void animate();
void setLoopToBegin(bool loop);
bool running();
void stop();
bool isSprite();
int getFrame();
int getFrameWidth();
int getFrameHeight();
int getWidth();
int getHeight();
int getSpriteWidth();
int getSpriteHeight();
bool equals(Sprite compare);
SDL_Surface* getSurface();
void setSurface(SDL_Surface* surface);
void destroy();
bool isTransparentPixel(int x, int y);
void setTransparentPixel(int x, int y);
Uint32 getPixel(int x, int y);
void setPixel(int x, int y, Uint32 color);
void setPixel(int x, int y, int red, int green, int blue);
Uint8 getPixel8(int x, int y);
void setPixel8(int x, int y, Uint8 color);
Uint16 getPixel16(int x, int y);
void setPixel16(int x, int y, Uint16 color);
Uint32 getPixel32(int x, int y);
void setPixel32(int x, int y, Uint32 color);
SDL_Surface* getRect(int x, int y, int width, int height);
bool rectCollide(int x1, int y1, Sprite &spriteB, int x2, int y2);
bool pixelCollide(int x1, int y1, Sprite &spriteB, int x2, int y2);
void rotate90();
void rotate180();
void rotate270();
void flipHorizontal();
void flipVertical();
void fade(float fade); // fade from 0 to 100%
void reverseAnimation();
void zoom(float z); // percentage to zoom in
void stretchX(float x); // percentage to stretchheight
void stretchY(float y); // percentage to stretchwidth
void stretch(float x,float y); // percentage to strech X and Y
bool loaded;
bool run;
int speed;
int width;
int height;
int index;
int indexIterator;
bool loopToBeginning; // if loop = true iterate through animations from 0 to N, then reset to 0
// if loop = false iterate through animations from 0 to N, then from N to 0, and repeat
int maxFrames;
int lastAnimated;
SDL_Surface* sprite;
private:
/* static helper functions */
static SDL_Surface* getRect(Sprite &sprite, int x, int y, int width, int height);
static bool rectCollide(Sprite &spriteA, int x1, int y1, Sprite &spriteB, int x2, int y2);
static bool pixelCollide(Sprite &spriteA, int x1, int y1, Sprite &spriteB, int x2, int y2);
static void rotate90(Sprite &sprite);
static void rotate180(Sprite &sprite);
static void rotate270(Sprite &sprite);
static void flipHorizontal(Sprite &sprite);
static void flipVertical(Sprite &sprite);
static void rotate(Sprite &sprite, int dir); // helper function for rotate90(),rotate180(), and rotate270()
static void flip(Sprite &sprite, int dir); // helper function for flipHorizontal() and flipVeritcal()
static void fade(Sprite &sprite, float fade); // fade from 0 to 100%
static void reverseAnimation(Sprite &sprite);
static void zoom(Sprite &sprite, float x); // percentage to zoom in
static void stretchX(Sprite &sprite, float x); // percentage to stretchheight
static void stretchY(Sprite &sprite, float y); // percentage to stretchwidth
static void stretch(Sprite &sprite, float x,float y); // percentage to strech X and Y
static Uint32 getPixel(SDL_Surface* sprite, int x, int y);
static void setPixel(SDL_Surface* sprite, int x, int y, Uint32 color);
};
#endif

View File

@ -0,0 +1,214 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9,00"
Name="cppcafe"
ProjectGUID="{20D8BAE2-2505-44AE-AA1A-C9A2A521BA48}"
RootNamespace="cppcafe"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="SDL.lib SDLmain.lib"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="2"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\main.cpp"
>
</File>
<File
RelativePath=".\Sprite.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\main.h"
>
</File>
<File
RelativePath=".\Sprite.h"
>
</File>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
<File
RelativePath=".\sprites\porta.bmp"
>
</File>
<File
RelativePath=".\sprites\sprites.bmp"
>
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioUserFile
ProjectType="Visual C++"
Version="9,00"
ShowAllFiles="false"
>
<Configurations>
<Configuration
Name="Debug|Win32"
>
<DebugSettings
Command="$(TargetPath)"
WorkingDirectory=""
CommandArguments=""
Attach="false"
DebuggerType="3"
Remote="1"
RemoteMachine="GIBSON"
RemoteCommand=""
HttpUrl=""
PDBPath=""
SQLDebugging=""
Environment=""
EnvironmentMerge="true"
DebuggerFlavor=""
MPIRunCommand=""
MPIRunArguments=""
MPIRunWorkingDirectory=""
ApplicationCommand=""
ApplicationArguments=""
ShimCommand=""
MPIAcceptMode=""
MPIAcceptFilter=""
/>
</Configuration>
<Configuration
Name="Release|Win32"
>
<DebugSettings
Command="$(TargetPath)"
WorkingDirectory=""
CommandArguments=""
Attach="false"
DebuggerType="3"
Remote="1"
RemoteMachine="GIBSON"
RemoteCommand=""
HttpUrl=""
PDBPath=""
SQLDebugging=""
Environment=""
EnvironmentMerge="true"
DebuggerFlavor=""
MPIRunCommand=""
MPIRunArguments=""
MPIRunWorkingDirectory=""
ApplicationCommand=""
ApplicationArguments=""
ShimCommand=""
MPIAcceptMode=""
MPIAcceptFilter=""
/>
</Configuration>
</Configurations>
</VisualStudioUserFile>

147
cppcafe/cppcafe/main.cpp Normal file
View File

@ -0,0 +1,147 @@
#include "main.h"
#define SCREEN_WIDTH 256
#define SCREEN_HEIGHT 200
using namespace std;
int main ( int argc, char* argv[] )
{
bool running = true;
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
printf( "Incapaz de inicializar o SDL: %s\n", SDL_GetError() );
running = false;
}
atexit(SDL_Quit);
/* Cria a screen */
SDL_Surface* screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16,
SDL_HWSURFACE|SDL_DOUBLEBUF);
SDL_WM_SetCaption( "Open Paradise Cafe", "Open Paradise Cafe" );
if ( !screen )
{
printf("Incapaz de inicializar a %ix%i: %s\n", SCREEN_WIDTH, SCREEN_HEIGHT, SDL_GetError() );
running = false;
}
SDL_Event keyevent;
/* Cria-se o Heroi e define-se a cor da transparencia */
Sprite Hero("sprites/sprites.bmp",4,300);
Hero.setTransparency(255,0,255);
/* Cria-se a Porta */
Sprite Porta("sprites/porta.bmp",5,300);
/* Parede */
SDL_Rect Wall;
Wall.x = 0;
Wall.y = 0;
Wall.w = SCREEN_WIDTH;
/*Wall.h = 136;*/
Wall.h = SCREEN_HEIGHT*0.68;
/* Chão */
SDL_Rect Floor;
Floor.x = 0;
/*Floor.y = 136;*/
Floor.y = SCREEN_HEIGHT*0.68;
Floor.w = SCREEN_WIDTH;
Floor.h = 24;
/* Linha do HiScore */
SDL_Rect HiScore;
HiScore.x = 0;
HiScore.y = 160;
HiScore.w = SCREEN_WIDTH;
HiScore.h = 8;
/* Status */
SDL_Rect Status;
Status.x = 0;
/*Status.y = 262 + 24;*/
Status.y = 168;
Status.w = SCREEN_WIDTH;
/*Status.h = SCREEN_HEIGHT - 262 - 24;*/
Status.h = 40;
/* Posição em X da Porta */
int PortaX = 350;
while(running)
{
/* Parede */
SDL_FillRect(screen, &Wall, SDL_MapRGB(screen->format, 0xc8, 0x41, 0x34));
/* Chão */
SDL_FillRect(screen, &Floor, SDL_MapRGB(screen->format, 0xc6, 0x0, 0xc6));
/* HiScore */
SDL_FillRect(screen, &HiScore, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
/* Status */
SDL_FillRect(screen, &Status, SDL_MapRGB(screen->format, 0x0, 0x0, 0xc6));
/* Desenha a Porta */
Porta.draw(screen,PortaX,0);
/* Desenha o Heroi */
/*Hero.draw(screen,150,150);*/
Hero.draw(screen,50,24);
/* Se chegar ao fim da linha - vai po inicio */
if (PortaX < 0)
{
PortaX = SCREEN_WIDTH;
}
/* Se a porta colidir com o Heroi */
if ( Porta.pixelCollide(PortaX,50,Hero,50,24) )
{
/* Faz a animação da porta ate ao fim */
if (Porta.getFrame() < 4)
{
Porta.animate();
}
}
else
{
/* Animação do Heroi e movimento da Porta */
Hero.animate();
PortaX -= 10;
}
SDL_Flip(screen);
/* Aguarda um momento */
SDL_Delay(200);
SDL_PollEvent(&keyevent);
switch(keyevent.type)
{
case SDL_KEYDOWN:
printf("key down\n");
switch(keyevent.key.keysym.sym)
{
case SDLK_ESCAPE:
running = false;
break;
default:
break;
}
break;
case SDL_QUIT:
running = false;
break;
default:
break;
}
}
return 0;
}

19
cppcafe/cppcafe/main.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef main_h
#define main_h
#include "Sprite.h"
/*
Sprite library for SDL - using bitmaps
This library was put together by Kenny Cason and is designed to
be easily implemented into any C++ program using SDL
Feel free to do what ever you want with it. enjoy!
Please report any bugs
kenneth [DOT] cason [AT] gmail [DOT] com
v1.0
2009 Sep 20
www.ken-soft.com
*/
#endif

View File

Before

Width:  |  Height:  |  Size: 112 KiB

After

Width:  |  Height:  |  Size: 112 KiB

View File

Before

Width:  |  Height:  |  Size: 60 KiB

After

Width:  |  Height:  |  Size: 60 KiB

BIN
original/porta.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

BIN
original/sprites.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB