#include #include #include #include #include "texture.h" //MAP DRAWING VARIABLES //********************* int scrollx, scrolly; int xoff, yoff; char map_layer1[500][500]; char map_layer2[500][500]; Texture layer1[30]; Texture layer2[30]; //********************* //Initial window height int initial_height = 0; bool LoadTGA(Texture *, char *); void init(void); void display(void); float menu_r = 0; float menu_g = 0; float menu_b = 0; GLuint base = 0; #define ScreenWidth 0 #define ScreenHeight 1 int Grab(char GrabParam) { if(GrabParam==ScreenHeight) { if(glutGet(GLUT_SCREEN_WIDTH)==640) //If the screen resolution is 640x480 { return(glutGet(GLUT_SCREEN_HEIGHT)-18); } if(glutGet(GLUT_SCREEN_WIDTH)==800) //If the screen resolution is 800x600 { return(glutGet(GLUT_SCREEN_HEIGHT)-10); } if(glutGet(GLUT_SCREEN_WIDTH)==1024) //If the screen resolution is 1024x768 { return(glutGet(GLUT_SCREEN_HEIGHT)); } if(glutGet(GLUT_SCREEN_WIDTH)>1024) //If the screen resolution is greater than 1024x768 { return(glutGet(GLUT_SCREEN_HEIGHT)+9); } } } void resize(int width, int height) { // float ratio; // ratio = 1.0*width/height; glColor3f(1.0f, 1.0f, 1.0f); //glMatrixMode(GL_MODELVIEW); //glLoadIdentity(); //gluPerspective(25.0f,ratio ,-1.0f,1.0f); //gluLookAt(0.0f, 0.0f, 50.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); // glViewport(0, 0, glutGet(GLUT_SCREEN_WIDTH), glutGet(GLUT_SCREEN_HEIGHT)); ///////glViewport(0, 0, glutGet(GLUT_SCREEN_WIDTH), Grab(ScreenHeight)); glutPostRedisplay(); } Texture FontTex[100]; void LoadGUI() { if ( LoadTGA(&FontTex[0], "panels/tile_pnl.tga")&& LoadTGA(&FontTex[1], "font.tga")&& LoadTGA(&FontTex[2], "panels/project_pnl.tga")&& LoadTGA(&FontTex[3], "panels/map_pnl.tga") && LoadTGA(&FontTex[4], "menu.tga") ) { for (int loop=0; loop<100; loop++) // Loop Through all Textures { glGenTextures(1, &FontTex[loop].texID); // Create The Textures glBindTexture(GL_TEXTURE_2D, FontTex[loop].texID); gluBuild2DMipmaps(GL_TEXTURE_2D, FontTex[loop].type, FontTex[loop].width, FontTex[loop].height, FontTex[loop].type, GL_UNSIGNED_BYTE, FontTex[loop].imageData); //glTexImage2D(GL_TEXTURE_2D, 0, FontTex[loop].type, FontTex[loop].width, FontTex[loop].height, 0, //FontTex[loop].type, GL_UNSIGNED_BYTE, FontTex[loop].imageData); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR); if (FontTex[loop].imageData) // If Texture Image Exists ( CHANGE ) { free(FontTex[loop].imageData); // Free The Texture Image Memory ( CHANGE ) } } } } Texture Buttons[100]; void LoadButtons() { if ( LoadTGA(&Buttons[0], "buttons/draw_u.tga")&& LoadTGA(&Buttons[1], "buttons/draw_p.tga") && LoadTGA(&Buttons[2], "buttons/fill_u.tga") && LoadTGA(&Buttons[3], "buttons/fill_p.tga") && LoadTGA(&Buttons[4], "buttons/zoom_u.tga") && LoadTGA(&Buttons[5], "buttons/zoom_p.tga") ) { for (int loop=0; loop<100; loop++) // Loop Through all Textures { glGenTextures(1, &Buttons[loop].texID); // Create The Textures glBindTexture(GL_TEXTURE_2D, Buttons[loop].texID); gluBuild2DMipmaps(GL_TEXTURE_2D, Buttons[loop].type, Buttons[loop].width, Buttons[loop].height, Buttons[loop].type, GL_UNSIGNED_BYTE, Buttons[loop].imageData); //glTexImage2D(GL_TEXTURE_2D, 0, FontTex[loop].type, FontTex[loop].width, FontTex[loop].height, 0, //FontTex[loop].type, GL_UNSIGNED_BYTE, FontTex[loop].imageData); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); if (Buttons[loop].imageData) // If Texture Image Exists ( CHANGE ) { free(Buttons[loop].imageData); // Free The Texture Image Memory ( CHANGE ) } } } } int window=0; void Idle() { } int main(int argc, char *argv[]) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA); glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)-8,glutGet(GLUT_SCREEN_HEIGHT)-50); glutInitWindowPosition(0,0); window = glutCreateWindow("RPG-Forge Map Editor"); init(); glutDisplayFunc(display); glutReshapeFunc(resize); glutMainLoop(); return 0; } GLvoid BuildFont() { float cx, cy; //Holds font X and Y coordinates int loop=0; base=glGenLists(256); glBindTexture(GL_TEXTURE_2D, FontTex[1].texID); for(loop=0;loop<256; loop++) { cx = float(loop%16)/16.0; cy = float(loop/16)/16.0; glNewList(base + loop, GL_COMPILE); glBegin(GL_QUADS); glTexCoord2f(cx,1-cy-0.0625f); glVertex2f(0,0); glTexCoord2f(cx+0.0625f,1-cy-0.0625f); glVertex2f(0.040,0); glTexCoord2f(cx+0.0625f,1-cy); glVertex2f(0.040,0.040); glTexCoord2f(cx,1-cy); glVertex2f(0.0,0.040); glEnd(); glTranslatef(0.020,0,0); //Letter spacing glEndList(); } } GLvoid KillFont(GLvoid) // Delete The Font From Memory { glDeleteLists(base,256); // Delete All 256 Display Lists } GLvoid glPrint(float x, float y, char *string, char set) { if(set>1) { set = 1; } glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.55); glBindTexture(GL_TEXTURE_2D, FontTex[1].texID); glTranslatef(x/40,y/50, 0.0); glListBase(base-32+(128*set)); glCallLists(strlen(string),GL_BYTE,string); glDisable(GL_ALPHA_TEST); } GLvoid glPrintShadow(float x, float y, char *string, char set, float shd_clr[3], float text_clr[3]) { if(set>1) { set = 1; } glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.55); glBindTexture(GL_TEXTURE_2D, FontTex[1].texID); glPushMatrix(); glColor3f(shd_clr[0], shd_clr[1], shd_clr[2]); glTranslatef(x/40,y/50, 0.0); glListBase(base-32+(128*set)); glCallLists(strlen(string),GL_BYTE,string); glPopMatrix(); glPushMatrix(); glColor3f(text_clr[0], text_clr[1], text_clr[2]); glTranslatef(x/40,y/50, 0.0); glTranslatef(0.003, 0.002, 0.0); glListBase(base-32+(128*set)); glCallLists(strlen(string),GL_BYTE,string); glDisable(GL_ALPHA_TEST); glPopMatrix(); } void init(void) { glClearColor(0.75f ,0.75f ,0.75f, 1.0f); //Win32 Gray //glClearColor(0.05f ,0.25f ,0.35f, 1.0f); //Background color //glClearColor(0.55f ,0.75f ,0.85f, 1.0f); //Background color // glClearColor(0.05f ,0.05f ,0.75f ,0.0f); // glColor3f(1.0f,1.0f,1.0f); glEnable(GL_TEXTURE_2D); glEnable(GL_ALPHA_TEST); glLineWidth(1); // glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // glViewport (0, 0, glutGet(GLUT_SCREEN_WIDTH), glutGet(GLUT_SCREEN_HEIGHT)); // glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, 1024, 0, 768, 0.0f, 0.0f); glViewport(0, 0, glutGet(GLUT_SCREEN_WIDTH), Grab(ScreenHeight)); initial_height = glutGet(GLUT_WINDOW_HEIGHT); //gluOrtho2D (0, 1024, 0, 768); // glMatrixMode(GL_MODELVIEW); // glLoadIdentity(); // gluPerspective(45.0f,640.0/480.0,-1.0f,1.0f); // gluLookAt(0.0f, 0.0f, 50.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); //glTranslatef(-0.99,0.30,0); glTranslatef(-1.0,-1.0,0); LoadGUI(); //Loads GUI textures (TGA Files) LoadButtons(); //Loads all the Button TGA-files BuildFont(); } void draw_GUI() { glPushMatrix(); glTranslatef(0.005, 0.6, 0.0); glBindTexture(GL_TEXTURE_2D, FontTex[0].texID); glBegin(GL_QUADS); glTexCoord2i(0, 0); glVertex2f(0.0, 0.0); glTexCoord2i(0, 1); glVertex2f(0.0, 1.145); glTexCoord2i(1, 1); glVertex2f(0.495, 1.145); glTexCoord2i(1, 0); glVertex2f(0.495, 0.0); glEnd(); //Draw Button glTranslatef(0.03, 1.05, 0.0); glBindTexture(GL_TEXTURE_2D, Buttons[0].texID); glBegin(GL_QUADS); glTexCoord2i(0, 0); glVertex2f(0.0, 0.0); glTexCoord2i(0, 1); glVertex2f(0.0, 0.063); glTexCoord2i(1, 1); glVertex2f(0.0535, 0.063); glTexCoord2i(1, 0); glVertex2f(0.0535, 0.0); glEnd(); //Zoom Tool glTranslatef(0.053, 0.0, 0.0); glBindTexture(GL_TEXTURE_2D, Buttons[2].texID); glBegin(GL_QUADS); glTexCoord2i(0, 0); glVertex2f(0.0, 0.0); glTexCoord2i(0, 1); glVertex2f(0.0, 0.063); glTexCoord2i(1, 1); glVertex2f(0.0535, 0.063); glTexCoord2i(1, 0); glVertex2f(0.0535, 0.0); glEnd(); //Fill Tool glTranslatef(0.053, 0.0, 0.0); glBindTexture(GL_TEXTURE_2D, Buttons[4].texID); glBegin(GL_QUADS); glTexCoord2i(0, 0); glVertex2f(0.0, 0.0); glTexCoord2i(0, 1); glVertex2f(0.0, 0.063); glTexCoord2i(1, 1); glVertex2f(0.0535, 0.063); glTexCoord2i(1, 0); glVertex2f(0.0535, 0.0); glEnd(); glPopMatrix(); glPushMatrix(); glTranslatef(0.005, 0.05, 0.0); glBindTexture(GL_TEXTURE_2D, FontTex[2].texID); glBegin(GL_QUADS); glTexCoord2i(0, 0); glVertex2f(0.0, 0.0); glTexCoord2i(0, 1); glVertex2f(0.0, 0.535); glTexCoord2i(1, 1); glVertex2f(0.495, 0.535); glTexCoord2i(1, 0); glVertex2f(0.495, 0.0); glEnd(); glPopMatrix(); glPushMatrix(); glTranslatef(0.5075, 0.05, 0.0); glBindTexture(GL_TEXTURE_2D, FontTex[3].texID); glBegin(GL_QUADS); glTexCoord2i(0, 0); glVertex2f(0.0, 0.0); glTexCoord2i(0, 1); glVertex2f(0.0, 1.7); glTexCoord2i(1, 1); glVertex2f(1.48, 1.7); glTexCoord2i(1, 0); glVertex2f(1.48, 0.0); glEnd(); glPopMatrix(); ///TEMPORARY : File Menu glPushMatrix(); glTranslatef(0.003, 1.815, 0.0); glBindTexture(GL_TEXTURE_2D, FontTex[4].texID); glBegin(GL_QUADS); glTexCoord2i(0, 0); glVertex2f(0.0, 0.0); glTexCoord2i(0, 1); glVertex2f(0.0, 0.052); glTexCoord2i(1, 1); glVertex2f(0.28, 0.052); glTexCoord2i(1, 0); glVertex2f(0.28, 0.0); glEnd(); glPopMatrix(); } /*FILE *map_file; void SaveMap(char *filename) { map_file = fopen(filename, "wb"); if(!map_file) return; fwrite(map_data, sizeof(unsigned char), (2*MAP_SIZEX)*(MAP_SIZEY*2), map_file); fclose(map_file); } void LoadMap(char *filename) { map_file = fopen(filename, "rb"); if(!map_file) return; fread(map_data, sizeof(unsigned char), (2*MAP_SIZEX)*(2*MAP_SIZEY), map_file); fclose(map_file); //Parse map into 2 seperate layers int i, j, k, l; //Layer 1 for(i = 0; i < MAP_SIZEX; i++) { for(j = 0; j < MAP_SIZEY; j++) { map_layer1[j][i] = map_data[j][i]; } } //Layer 2 for(k = 0; k < MAP_SIZEX; k++) { for(l = 0; l < MAP_SIZEY; l++) { map_layer1[l][k] = map_data[l+MAP_SIZEY][k+MAP_SIZEX]; } } } */ void draw_map() { } void draw_tiles(char layer, int x, int y, GLfloat z) { int mapx=0, mapy=0; int tile; for(y=1;y<15*32;y+=32) { for(x=1;x<17*32;x+=32) { mapx = scrollx/32; mapy = scrolly/32; xoff = scrollx & 31; yoff = scrolly & 31; int y32, x32; y32 = y/32; x32 = x/32; if(layer==1) { tile = map_layer1[y32 + mapy][x32 + mapx]; glBindTexture(GL_TEXTURE_2D,Buttons[tile].texID); } else { if(layer==2) { tile = map_layer2[y32 + mapy][x32 + mapx]; glBindTexture(GL_TEXTURE_2D,layer2[tile].texID); } } //Faster : Triangle Strips rendering code glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(0.0, 0.0); glVertex3f(x-xoff, y-yoff, 0); glTexCoord2f(0.0, 1.0); glVertex3f(x-xoff, y-yoff + 32, 0); glTexCoord2f(1.0, 0.0); glVertex3f(x-xoff + 32, y-yoff, 0); glTexCoord2f(1.0, 1.0); glVertex3f(x-xoff + 32, y-yoff + 32, 0); glEnd(); // scrollx += 5; Scrambles tile positions... } } } void display(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); draw_GUI(); glPushMatrix(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(54.0f,640.0/480.0,-1.0f,1.0f); // gluLookAt(0.0f, 0.0f, 50.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glTranslated(-160, -230, -500); scrollx += 1; scrolly += 1; glEnable(GL_SCISSOR_TEST); //glScissor(GLint x,GLint y,GLsizei width,GLsizei height); /// glScissor(270,45,721,617); //COMMENT : The scissor must change depending on the window size //Initial height - Window Height is how much the scissor has to be moved //NOTE: That value is divided by 128 to increase the precision int new_height = glutGet(GLUT_WINDOW_HEIGHT); glScissor(270,45-(initial_height-new_height),721,617); //The scissor is now correct after resizing! draw_tiles(1, 0, 0, 1); glutPostRedisplay(); glDisable(GL_SCISSOR_TEST); glPopMatrix(); float shadow_color[3] = {0.0, 0.0, 0.0}; float text_color[3] = {1.0, 1.0, 1.0}; //glPrintShadow(6, 24, "Project", 0, shadow_color, text_color); glutSwapBuffers(); }