main.cpp
Código: Seleccionar todo
/***************************************************************************
Ejemplo5_1
(C) 2003 by Alberto Garcia Serrano
Programación de videojuegos con SDL
***************************************************************************/
#include <stdio>
#include <stdlib>
#include <SDL>
#include <SDL>
#include "sprite.h"
SDL_Surface *screen;
Frame fnave;
Frame fmalo;
Sprite nave(1);
Sprite malo(1);
SDL_Rect rectangulo;
int done=0;
// estructura que contiene la información
// de nuestro avión
struct minave {
int x,y;
} jugador;
// Estructura que contiene información
// del avión enemigo
struct naveenemiga {
int x,y;
} enemigo;
// Dibuja los esprites en la pantalla
void DrawScene(SDL_Surface *screen) {
// borramos el avión dibujado
// en el frame anterior
rectangulo.x=nave.VerX();
rectangulo.y=nave.VerY();
rectangulo.w=nave.VerAncho();
rectangulo.h=nave.VerAlto();
SDL_FillRect(screen,&rectangulo,SDL_MapRGB(screen->format,0,0,0));
// dibuja avión
nave.CargarX(jugador.x);
nave.CargarY(jugador.y);
nave.Dibujar(screen);
// Dibuja enemigo
malo.CargarX(enemigo.x);
malo.CargarY(enemigo.y);
malo.Dibujar(screen);
// ¿ha colisionado con la nave?
if (malo.ColisionCajas(nave) == TRUE) {
done=1;
}
// Mostramos todo el frame
SDL_Flip(screen);
}
// Inicializamos estados
void inicializa() {
jugador.x=300;
jugador.y=300;
enemigo.x=100;
enemigo.y=100;
}
void finaliza() {
// finalizamos los sprites
nave.Finalizar();
malo.Finalizar();
}
// Preparamos los esprites
int InitSprites() {
fnave.Cargar("cuadro2.png");
//fnave.Mascara(); // Aqui se genera el error
nave.AgregarFrame(fnave);
fmalo.Cargar("cuadro1.png");
malo.AgregarFrame(fmalo);
return 0;
}
int main(int argc, char *argv[]) {
SDL_Event event;
Uint8 *keys;
if (SDL_Init(SDL_INIT_VIDEO) <0> 0)) {jugador.y=jugador.y-(1);}
if ((keys[SDLK_DOWN]) && (jugador.y <460> 0)) {jugador.x=jugador.x-(1);}
if ((keys[SDLK_RIGHT]) && (jugador.x < 620)) {jugador.x=jugador.x+(1);}
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {done=1;}
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_ESCAPE) {
done=1;
}
}
}
}
finaliza();
return 0;
}
Código: Seleccionar todo
/*----------------------------------------------------------------
| Autor: Exequiel Lares |
| Fecha: 26-06-2007 Versión: 1.0 |
|-----------------------------------------------------------------|
| Descripción del Programa: libreria para manejo de sprites |
| Archivo de cabecera |
| ----------------------------------------------------------------*/
#ifndef SPRITE_H_
#define SPRITE_H_
#define TRUE 1
#define FALSE 0
#include <SDL>
typedef unsigned long word;
typedef struct {
int LongX;
int LongY;
int Words;
word** MaskData;} mask;
class Frame{
public:
mask *m;
SDL_Surface *img;
void Cargar(char *path);
void Liberar();
int DestrMascara (mask *Mascara);
void Mascara();
void InitMascara (SDL_Surface *BitMap, mask *Mascara);
mask * CreaMascara (SDL_Surface *BitMap);
};
class Sprite{
private:
int x , y, estado, nframes, cont;
public:
Frame *sprite;
Sprite();
Sprite(int nf);
void Finalizar();
void AgregarFrame(Frame frame);
void ElegirFrame(int nf);
int CantFrames(){return cont;}
void CargarX(int xx){x=xx;}
void CargarY(int yy){y=yy;}
void IncrementaX(int c){x+=c;}
void IncrementaY(int c){y+=c;}
int VerX(){return x;}
int VerY(){return y;}
int VerAncho(){return sprite[estado].img->w;}
int VerAlto(){return sprite[estado].img->h;}
void Dibujar(SDL_Surface *Superficie);
bool ColisionCajas(Sprite sp);
bool ColisionPixel(Sprite sp);
};
SDL_Color getPixel(SDL_Surface *screen, int x, int y);
#endif /* SPRITE_H_ */
Código: Seleccionar todo
/*----------------------------------------------------------------
| Autor: Exequiel Lares |
| Fecha: 26-06-2007 Versión: 1.0 |
|-----------------------------------------------------------------|
| Descripción del Programa: libreria para manejo de sprites |
| con SDL |
| ----------------------------------------------------------------*/
#include <SDL>
#include <SDL>
#include "sprite.h"
//Metodos de la clase Frame
void Frame::Cargar(char *path)
{
img=IMG_Load(path); //Cargamos la imagen
SDL_SetColorKey(img, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(img->format,255,0,0)); // Seleccionamos el color transparente = ROJO
img=SDL_DisplayFormat(img); //Cambia el formato de la imagen por el formato de la pantalla (Ahorra tiempo)
}
void Frame::Liberar()
{
SDL_FreeSurface(img); //Libera la memoria, borra la superficie
}
mask * Frame::CreaMascara (SDL_Surface *BitMap)
{
int Tamano=sizeof(word)*8;
mask *Retorno;
int b1;
Retorno = new mask;
Retorno->LongX = BitMap->w;
Retorno->LongY = BitMap->h;
Retorno->Words = Retorno->LongX / Tamano;
if (Retorno->LongX % Tamano != 0) Retorno->Words += 1;
Retorno->MaskData = new word*[Retorno->LongY];
for (b1 = 0; b1 <Retorno>LongY; b1++)
Retorno->MaskData[b1] = new word [Retorno->LongX];
return Retorno;
}
void Frame::InitMascara (SDL_Surface *BitMap, mask *Mascara)
{
int Tamano=sizeof(word)*8;
int b1,b2,b3;
for (b1 = 0; b1 <Mascara>LongY; b1++)
for (b2 = 0; b2 <Mascara>Words; b2++)
Mascara->MaskData[b1][b2] = 0;
for (b1 = 0; b1 <Mascara>LongY; b1++)
for (b2 = 0; b2 <Mascara>Words; b2++)
for (b3 = 0; b3 <Tamano>MaskData[b1][b2]+= ( 1 <<Mascara>LongX - b2));
}
}
int Frame::DestrMascara (mask *Mascara)
{
int b1;
if (!Mascara) return -1;
for (b1 = 0; b1 <Mascara>LongY; b1++)
delete[] Mascara->MaskData[b1];
delete[] Mascara->MaskData;
delete Mascara;
if (Mascara) return 1;
return 0;
}
void Frame::Mascara()
{
InitMascara(img, m);
m=CreaMascara(img);
}
//Metodos de la clase Sprite
Sprite::Sprite(int nf)
{
sprite=new Frame[nf];
nframes=nf;
cont=0;
}
Sprite::Sprite()
{
int nf=1;
sprite=new Frame[nf];
nframes=nf;
cont=0;
}
void Sprite::Finalizar()
{
int i;
for(i=0; i<=nframes-1; i++)
sprite[i].Liberar();
}
void Sprite::AgregarFrame(Frame frame)
{
if(cont<nframes){
sprite[cont]=frame;
cont++;
}
}
void Sprite::ElegirFrame(int nf)
{
if(nf<nframes>x2) && ((y1+h1)>y2) && ((x2+w2)>x1) && ((y2+h2)>y1))
return true;
else
return false;
}
/*bool Sprite::ColisionPixel(Sprite sp)
{
}
*/
SDL_Color getPixel(SDL_Surface *screen, int x, int y)
{
SDL_Color color;
Uint32 col;
char *buffer=(char *) screen->pixels; // Determinamos posición de inicio
buffer+=screen->pitch*y; // Calculamos offset para y
buffer+=screen->format->BytesPerPixel*x; // Calculamos offset para x
memcpy(&col, buffer, screen->format->BytesPerPixel); // Obtenemos el pixel
SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b); // Descomponemos el color
return color;
}
Saludos y espero ansiosamente respuestas





