Editor De Mapas Src
ASDFASCIIASDF28 de Mayo de 2013
3.225 Palabras (13 Páginas)409 Visitas
#include "allegro5/allegro_font.h"
#include "allegro5/allegro_ttf.h"
#include "allegro5/allegro_native_dialog.h"
#include "src/base.h"
#include "src/button.h"
#include "src/mapa.h"
/*#define bbox_collision(x1,y1,w1,h1,x2,y2,w2,h2) (!( ((x1)>=(x2)+(w2)) || ((x2)>=(x1)+(w1)) || \
((y1)>=(y2)+(h2)) || ((y2)>=(y1)+(h1)) )) */
using namespace std;
ALLEGRO_DISPLAY *display;
ALLEGRO_TIMER *timer;
ALLEGRO_EVENT_QUEUE *event_queue;
ALLEGRO_EVENT event;
ALLEGRO_BITMAP *screen;
ALLEGRO_BITMAP *bTexturasGame1[48];
ALLEGRO_BITMAP *_button[9];
ALLEGRO_BITMAP *bLetras[64];
//ALLEGRO_BITMAP *_numeros[10];
ALLEGRO_FONT *font;
//const int pixel = 32;
const int width = pixel*35;
const int height = pixel*21;
//const int columna = 40, fila = 25;
const int screenWidth = pixel*23;
const int screenHeight = pixel*18;
const char letra[65] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPOQRSTUVWXYZ:.0123456789"};
bool eliminar = 0, enter = 0, guardaMapa = 0,guardaImagen= 0;
void set_texturas_to_mapTexturas( MAP_TEXTURAS *map_texturas, ALLEGRO_BITMAP *bitmap[] ){
for(int i= 0; i < map_texturas->numTexts ; i++){
al_set_target_bitmap( map_texturas->textura[i] );
al_draw_bitmap(bitmap[i], 0, 0, 0);
}
al_set_target_bitmap(al_get_backbuffer(display) );
}
void resaltar_cursor_on_matriz( Coord mousePosition, Coord mapPosition, int col, int fil,int pix ){
int i = 0, j= 0;
if( mousePosition.x >= mapPosition.x && mousePosition.y >= mapPosition.y)
if( check_collision_mouse_with_map( mousePosition, mapPosition, col, fil,pix , j, i) ){
al_draw_rectangle( mapPosition.x + j*pix, mapPosition.y + i*pix,
(mapPosition.x + j*pix)+pix, (mapPosition.y + i*pix)+pix, al_map_rgb(200,0,0), 0);
}
}
void dibujar_cuadricula_matriz(int x, int y, int W, int H){
int i;
// Pinta lines Verticales
for( i = 0; i <= W; i++){
al_draw_line(pixel*i +x, 0+y, pixel*i+x, H*pixel+y , al_map_rgb(255, 255, 255),0 );
}
// Pinta lines horizontales
for( i = 0; i <= H; i++){
al_draw_line(0+x , pixel*i+y, W*pixel+x , pixel*i+ y, al_map_rgb(255, 255, 255),0 );
}
}
void copiar_imagen_a_escala(ALLEGRO_BITMAP *copia, ALLEGRO_BITMAP *pega, int escalaW, int escalaH ){
al_set_target_bitmap (pega);
al_draw_scaled_bitmap(copia, 0,0, al_get_bitmap_width(copia), al_get_bitmap_height(copia)
,0,0, escalaW, escalaH,0);
al_set_target_bitmap(al_get_backbuffer(display) );
}
void dibujar_mapa(const char tileMapa[fila][columna], const MAP_TEXTURAS texturasEditor, int textuValores[] ,int x = 0, int y = 0){
int i= 0, j = 0, k = 0;
for( i = 0; i < fila; i++){ // Se dibuja el mapa de tildes
for( j = 0; j < columna; j++){
for(k = 1; k < texturasEditor.fila *texturasEditor.columna+1; k++){
if(tileMapa[i][j] == k ){
if(textuValores[k-1] >= 0 && textuValores[k-1] < texturasEditor.fila*texturasEditor.columna ){
al_draw_bitmap(texturasEditor.textura[textuValores[k-1] ],x+ j*pixel, y+ i*pixel, 0 );
}
}
}
}
}
}
void SAVE_CURRENT_MAP_IMAGE (const char *dir_and_name, char currentMap[fila][columna],MAP_TEXTURAS currentTexturas, int textsValor[] ){
ALLEGRO_BITMAP *buffer = al_create_bitmap(columna*pixel, fila *pixel);
al_set_target_bitmap(buffer);
dibujar_mapa(currentMap, currentTexturas, textsValor);
al_save_bitmap(dir_and_name, buffer);
al_destroy_bitmap(buffer);
al_set_target_bitmap( al_get_backbuffer(display) );
};
void SAVE_CURRENT_MAP(const char *dir_and_name, char tileMapa[fila][columna] ){
std::ofstream fout(dir_and_name);
fout << columna<<" "<< fila ;
for(int i= 0; i < fila; i++){
fout << std::endl;
for(int j= 0; j < columna; j++){
fout << int (tileMapa[i][j] )<<" ";
}
}
}
class PALABRA{
public:
PALABRA( int _tam = 1);
~PALABRA();
void reiniciar(int _tam);
//friend void imprimir(const PALABRA);
//friend void ingresar(PALABRA &);
int tam, cont;
char *palabra;
};
PALABRA::PALABRA(int _tam ){
tam = _tam;
palabra = new char[tam];
cont = 0;
palabra[cont] = '_';
for(int j=1; j < tam; j++)
palabra[j] = char(8);
palabra[tam] = ' ';
}
PALABRA::~PALABRA(){
delete []palabra;
}
void PALABRA::reiniciar(int _tam){
delete []palabra;
tam = _tam;
palabra = new char[tam];
cont = 0;
palabra[cont] = '_';
for(int j=1; j < tam; j++)
palabra[j] = char(8);
palabra[tam] = ' ';
}
void ingresar_texto(PALABRA &texto , char caract ){
texto.palabra[texto.cont] = caract;
if( texto.cont+1 < texto.tam ) texto.cont++;
if( texto.cont < texto.tam-1)texto.palabra[texto.cont] = '_';
}
void ingresar_cadena(PALABRA &cadena, MAP_TEXTURAS *letras,MOUSE_STATE mouse,bool &bloqueado ,int x = 0, int y = 0){
bool finalizado = 0;
for(int i= 0; i <= cadena.tam; i++){
if(cadena.palabra[i] == '\0') finalizado = 1;
}
if(!finalizado){
letras->actualizar( mouse );
if(enter){
if(cadena.cont == cadena.tam-1 ) cadena.palabra[cadena.tam] = '\0';
else cadena.palabra[cadena.cont] = '\0';
}
else if(letras->sobre && mouse.button.IZQ && !bloqueado ){
ingresar_texto(cadena, letra[letras->sel] );
bloqueado = 1;
}
else if(eliminar){
cadena.palabra[cadena.cont] = char(8);
if(cadena.cont > 0 && cadena.cont < cadena.tam ) cadena.cont--;
eliminar = 0;
}
if( !mouse.button.IZQ && bloqueado) bloqueado = 0;
int dy = al_get_bitmap_height(letras->textura[0] )*letras->fila;
letras->dibujar( x, y);
al_draw_textf(font, al_map_rgb(200, 0, 0), x, y + dy ,0, "%s",cadena.palabra );
}
}
bool cuadricula = 0, bloq = 1;
Dir camEfect = {0,0,0,0};
Coord editorPos = {0,0};
MOUSE_STATE mouse;
BUTTON *button[9];
MAP_TEXTURAS *texturasEditor;
MAP_TEXTURAS *textuLetras;
MAP_EDITOR *mapEditor;
PALABRA palabra(15);
int textsValors[48];
void program_init(){
for(int i= 0; i < 48; i++ ){
textsValors[i] = i;
}
texturasEditor = new MAP_TEXTURAS(8,6,pixel , 48);
textuLetras = new MAP_TEXTURAS(8, 8,22, 64);
//textuNumeros = new MAP_TEXTURAS(2, 5, 10);
set_texturas_to_mapTexturas( texturasEditor , bTexturasGame1 ); // inicializamos las texturas del mapa
set_texturas_to_mapTexturas( textuLetras, bLetras);
//set_texturas_to_mapTexturas( textuNumeros, _numeros);
mapEditor = new MAP_EDITOR;
for(int i= 0; i< 7; i++){
button[i] = new BUTTON(60, 40);
copiar_imagen_a_escala(_button[i], button[i]->imagen, 60, 40);
}
for(int i= 7; i < 9; i++){
button[i] = new BUTTON(35, 25 );
copiar_imagen_a_escala(_button[i], button[i]->imagen, 35, 25 );
}
//in = new INPUT_TEXT(10);
}
void program_update(){
// se actualizan los botones
if(!guardaImagen && !guardaMapa){
button[0]->actualizar_como_push(mouse, camEfect.LEFT);
button[1]->actualizar_como_push(mouse, camEfect.RIGHT);
button[2]->actualizar_como_push(mouse, camEfect.UP);
button[3]->actualizar_como_push(mouse, camEfect.DOWN);
button[4]->actualizar_como_interruptor(mouse, cuadricula);
}
if(!guardaImagen) button[5]->actualizar_como_interruptor( mouse, guardaMapa );
if(!guardaMapa) button[6]->actualizar_como_interruptor( mouse, guardaImagen );
if(guardaImagen || guardaMapa){
button[7]->actualizar_como_interruptor(mouse, eliminar);
button[8]->actualizar_como_interruptor(mouse,
...