Logo

¡Bienvenido a Stratos!

Acceder

Foros



Problema con Carga de Tilemap en C++ y GLut

Iniciado por Netto22, 28 de Diciembre de 2009, 12:00:46 PM

« anterior - próximo »

Netto22

Buenas,

Tengo una pequeña consulta con esto porque el tilemapping no me sale, y me gustaria saber como poder implementarlo. Os adjunto los archivos de mi codigo a ver si me podeis hechar una mano con ello.

Se que necesito las sentencias RenderImage(), tengo una estructura TileMap, y el RenderTileMap.

Os dejo tambien el codigo tal como lo he encontrado pero no se por donde tirar.

struct TileMap
{
int tile_width, //Ancho y alto de las tiles
tile_height; //(píxeles)
int map_height, //Ancho y alto de todo el mapa
map_width; //(tiles)
int scene_width, //Ancho y alto de la parte visible
scene_height; //por pantalla del mapa (tiles)
int coord_x, //Coordenadas de la parte visible
coord_y; //(píxeles)
Image *images; //Vector de imágenes para cada tile
int **data; //El mapa, la matriz de tiles
};


Creo que  RenderTileMap tiene que ser algo asi:

void RenderTileMap( TileMap *map )
{
int tile;
int Xo = map->coord_x / map->tile_width;
int Yo = map->coord_y / map->tile_height;
int Xf = Xo + map->scene_width - 1;
int Yf = Yo + map->scene_height - 1;
int pos_x = 0;
int pos_y = 0;
for(int i=Yo; i<=Yf; i++)
{
for(int j=Xo; j<=Xf; j++)
{
tile = map->data[i][j];
RenderImage(map->images[tile], pos_x, pos_y);
pos_x += map->tile_width;
}
pos_x = 0;
pos_y += map->tile_height;
}
}


Pero el RenderImage como no sea el dibujado que tengo en el Scene.cpp Draw(); sin animacion no se por donde tirar...

os adjunto por aqui el contenido de cada archivo que tengo.

CScene.cpp
#include "cScene.h"
//#include <GL/glut.h>

cScene::cScene(void)
{
}

cScene::~cScene(void)
{
}


bool cScene::LoadLevel(int level)
{
bool res;

// load tile based map
//....

// Imagenes
res = img.Load("caba.png", GL_RGBA);
frame_seq = 0;
frame_delay = 0;

return res;
}

//Dibujado
void cScene::Draw()
{
float frame;

frame = (float)frame_seq * 0.25f;
glBindTexture(GL_TEXTURE_2D,img.GetID());
glBegin(GL_QUADS);
glTexCoord2f(frame      ,0.5f); glVertex2i(100          ,100          );
glTexCoord2f(frame+0.25f,0.5f); glVertex2i(100+TILE_SIZE,100          );
glTexCoord2f(frame+0.25f,0.25f); glVertex2i(100+TILE_SIZE,100+TILE_SIZE);
glTexCoord2f(frame      ,0.25f); glVertex2i(100          ,100+TILE_SIZE);
glEnd();

glBegin(GL_QUADS);
glTexCoord2f(frame      ,0.5f); glVertex2i(200          ,200          );
glTexCoord2f(frame+0.25f,0.5f); glVertex2i(200+TILE_SIZE,200          );
glTexCoord2f(frame+0.25f,0.25f); glVertex2i(200+TILE_SIZE,200+TILE_SIZE);
glTexCoord2f(frame      ,0.25f); glVertex2i(200          ,200+TILE_SIZE);
glEnd();

glBegin(GL_QUADS);
glTexCoord2f(frame      ,0.5f); glVertex2i(300          ,300          );
glTexCoord2f(frame+0.25f,0.5f); glVertex2i(300+TILE_SIZE,300          );
glTexCoord2f(frame+0.25f,0.25f); glVertex2i(300+TILE_SIZE,300+TILE_SIZE);
glTexCoord2f(frame      ,0.25f); glVertex2i(300          ,300+TILE_SIZE);
glEnd();


glBegin(GL_QUADS);
glTexCoord2f(frame      ,0.5f); glVertex2i(400          ,400          );
glTexCoord2f(frame+0.25f,0.5f); glVertex2i(400+TILE_SIZE,400          );
glTexCoord2f(frame+0.25f,0.25f); glVertex2i(400+TILE_SIZE,400+TILE_SIZE);
glTexCoord2f(frame      ,0.25f); glVertex2i(400          ,400+TILE_SIZE);
glEnd();
frame_delay++;
if(frame_delay == ANIMATION_DELAY)
{
frame_delay = 0;
frame_seq++;
if(frame_seq == 4) frame_seq = 0;

}

}
int* cScene::GetMap()
{
return map;
}


cScene.h
#pragma once
#include "cTexture.h"
#define SCENE_ALTO 64
#define SCENE_ANCHO 64
#define TILE_SIZE 32
#define ANIMATION_DELAY 8

class cScene
{
public:
cScene(void);
virtual ~cScene(void);
// Carga del nivel de la textura en este caso solo 1
bool LoadLevel(int level);
// Accion de dibujado en pantalla
void Draw();

int *GetMap();

private:
int map[SCENE_ALTO*SCENE_ANCHO];

cTexture img;
int frame_seq;
int frame_delay;

};


main.cpp
#include <GL/glut.h>
#include "cGame.h"

#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")


cGame Game;


void AppRender()
{
Game.Render();
}
void AppKeyboard(unsigned char key, int x, int y)
{
Game.ReadKeyboard(key,x,y,true);
}
void AppKeyboardUp(unsigned char key, int x, int y)
{
Game.ReadKeyboard(key,x,y,false);
}
void AppSpecialKeys(int key, int x, int y)
{
Game.ReadKeyboard(key,x,y,true);
}
void AppSpecialKeysUp(int key, int x, int y)
{
Game.ReadKeyboard(key,x,y,false);
}
void AppMouse(int button, int state, int x, int y)
{
Game.ReadMouse(button,state,x,y);
}
void AppIdle()
{
if(!Game.Loop()) exit(0);
}

void main(int argc, char** argv)
{
int res_x,res_y,pos_x,pos_y;

//GLUT initialization
glutInit(&argc, argv);

//RGBA with double buffer
glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA | GLUT_DOUBLE);

//Create centered window
res_x = glutGet(GLUT_SCREEN_WIDTH);
res_y = glutGet(GLUT_SCREEN_HEIGHT);
pos_x = (res_x>>1)-(GAME_WIDTH>>1);
pos_y = (res_y>>1)-(GAME_HEIGHT>>1);

glutInitWindowPosition(pos_x,pos_y);
glutInitWindowSize(GAME_WIDTH,GAME_HEIGHT);
glutCreateWindow("My first game!");

/*glutGameModeString("800x600:32");
glutEnterGameMode();*/

//Make the default cursor disappear
//glutSetCursor(GLUT_CURSOR_NONE);

//Register callback functions
glutDisplayFunc(AppRender);
glutKeyboardFunc(AppKeyboard);
glutKeyboardUpFunc(AppKeyboardUp);
glutSpecialFunc(AppSpecialKeys);
glutSpecialUpFunc(AppSpecialKeysUp);
glutMouseFunc(AppMouse);
glutIdleFunc(AppIdle);

//Game initializations
Game.Init();

//Application loop
glutMainLoop();
}


Gracias y perdonad el tocho.






Stratos es un servicio gratuito, cuyos costes se cubren en parte con la publicidad.
Por favor, desactiva el bloqueador de anuncios en esta web para ayudar a que siga adelante.
Muchísimas gracias.
Stratos es un servicio gratuito, cuyos costes se cubren en parte con la publicidad.
Por favor, desactiva el bloqueador de anuncios en esta web para ayudar a que siga adelante.
Muchísimas gracias.