/ Published in: C
Expand |
Embed | Plain Text
//#include "GL/glew.h" #include "SDL/SDL.h" #include "SDL/SDL_ttf.h" #include "SDL/SDL_opengl.h" #include <math.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <time.h> //#define M_PI 3.1415296 // astyle -SUET81 /* LINUX gcc main.c -Wall -g -o hex -lGL -lGLU -lSDL */ /* WIN @echo off PATH = ..\..\b\MinGW\bin; set libs=-lmingw32 -lglew32 -lSDLmain -lSDL -lSDL_image -lSDL_ttf -lopengl32 -lglu32 set IN=main.c set OUT=hex.exe @echo on gcc.exe %IN% -pg -o %OUT% %libs% && hex.exe */ GLUquadricObj *gliquadric; float camera_x=0, camera_y=0; #define MODE_SELECT 1 #define MODE_MOVE 2 #define MODE_ATACK 3 #define MODE_PAUSE 4 int MODE = MODE_SELECT; int PLAYER = 1; int cursorX, cursorY; int aX = 99, aY = 99; // active cell int sX = 99, sY = 99; //selected cell bool IS_SOMEONE_SELECTED = false; float ROTATE_ANGLE = 0; float ROTATE_ANGLE_2 = 60; float HEX_EX; float HEX_IN; #define x_count (6) #define y_count (6) #define ushort unsigned short int #define uint unsigned int int PICKED_SMTH; bool isProgramLooping = true; SDL_Surface *surface; int video_flags; /* Flags to pass to SDL_SetVideoMode */ int FRAME = 0; int SCR_WIDTH = 640; int SCR_HEIGHT = 480; int SCR_BPP = 32; #define AFTERSCAN_PRINT 0 #define ROT_BY_KEYS 0 #define NO_FILL 0 #define RESIZE 1 #define resize_coefficient (0.20) #define V_COUNT 3000 #define VT_COUNT 3000 #define VN_COUNT 3000 #define F_COUNT 3000 TTF_Font* font; typedef struct { float x, y, z; } vector; typedef struct { float u, v; } texture_coords; typedef struct { int v1, v2, v3, vt1, vt2, vt3, vn1, vn2, vn3; } indexes; typedef struct { vector vertexes[V_COUNT ]; vector normals[VN_COUNT]; texture_coords text_coords[VT_COUNT]; indexes faces[F_COUNT ]; int f_count; } model; typedef struct { //int x, y; // map position int health; int player; float rot; int frame; //bool can_atack; //bool can_move; ushort ap; //acton points } character; typedef struct cell cell; struct cell { uint init_cost; uint move_cost; ushort parent_x, parent_y; character *ch; }; cell MAP[x_count][y_count]; model mdl[32]; model mdl_static; GLuint texture; int bx; int by; int ex; int ey; float rbx; float rby; float rex; float rey; float SPECIAL_X; float SPECIAL_Y; float SPECIAL_SPEED; float GIPATINUZA; float SPECIAL_ANGLE; float SPECIAL_STEP_X; float SPECIAL_STEP_Y; int SPECIAL_STEPS; character *TMP_CHAR; typedef struct { int x, y; //position int sx, sy; //size char *name; unsigned textureID; int textSX, textSY; //colorFG //colorBG } gui_button; int current_button = 0; gui_button BUTTONS[100]; void map_to_real(int mx, int my, float *rx, float *ry) { *rx = (2 * HEX_IN * mx); if (my%2 != 0) *rx -= HEX_IN; *ry = 1.5 * HEX_EX * my; } void translate(ushort y, ushort x) { float ry, rx; map_to_real(x, y, &rx, &ry); glTranslatef(rx, ry, 0); } void START_MOVE(int bX, int bY, int eX, int eY) { bx = bX; by = bY; ex = eX; ey = eY; map_to_real(bx, by, &rbx, &rby); map_to_real(ex, ey, &rex, &rey); SPECIAL_X = rbx; SPECIAL_Y = rby; SPECIAL_SPEED = 0.031; GIPATINUZA = sqrt(pow(rey-rby, 2) + pow(rex-rbx, 2)); SPECIAL_ANGLE = (asin((rex-rbx)/GIPATINUZA))/ M_PI * 180.0; if (ey-by > 0) SPECIAL_ANGLE += 180; SPECIAL_STEP_X = SPECIAL_SPEED * ((rex-rbx)/GIPATINUZA); SPECIAL_STEP_Y = SPECIAL_SPEED * ((rey-rby)/GIPATINUZA); SPECIAL_STEPS = (int)(GIPATINUZA/SPECIAL_SPEED); } typedef struct Q Q; struct Q { long int tail; long int head; ushort *qx; ushort *qy; long int qu_size; }; Q q; void push(Q *q, ushort x, ushort y, ushort px, ushort py, uint NEW) { MAP[x][y].move_cost = NEW; MAP[x][y].parent_x = px; MAP[x][y].parent_y = py; if (q->tail == q->qu_size) { q->tail = 0; } q->qx[q->tail] = x; q->qy[q->tail] = y; (q->tail)++; } void try_push(Q *q, ushort x, ushort y, ushort px, ushort py) { if ((x < x_count) && (y < y_count)) { //(x >= 0 && y >= 0) but ushort uint NEW = MAP[px][py].move_cost + MAP[x][y].init_cost; if ((MAP[x][y].move_cost > NEW)) //if(OLD > NEW) push(q, x, y, px, py, NEW); } } bool pop(Q *q, ushort *x, ushort *y) { if (q->head == q->tail) { return(false); // Q is empty } *x = q->qx[q->head]; *y = q->qy[q->head]; (q->head)++; if (q->head == q->qu_size) q->head = 0; return(true); } bool init_q(Q *q, long int size) { q->qx = (malloc(size * sizeof(ushort))); q->qy = (malloc(size * sizeof(ushort))); if (q->qx == NULL || q->qy == NULL) return(false); q->tail = 0; q->head = 0; q->qu_size = size; return(true); } void load_texture(/*int texnum, */char *name) { SDL_Surface *tmp_surface; // GLenum textureFormat; //tmp_surface = IMG_Load(name); tmp_surface = SDL_LoadBMP(name); #if 0 if (tmp_surface->format->BytesPerPixel == 4) { if (SDL_BYTEORDER == SDL_BIG_ENDIAN) textureFormat = GL_BGRA; else textureFormat = GL_RGBA; } else { if (SDL_BYTEORDER == SDL_BIG_ENDIAN) textureFormat = GL_BGR; else textureFormat = GL_RGB; } #endif /* Genarate texture */ //glGenTextures(1, &texture[texnum]); glGenTextures(1, &texture); //glBindTexture(GL_TEXTURE_2D, texture[texnum]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, tmp_surface->format->BytesPerPixel, //tmp_surface->w, tmp_surface->h, 0, textureFormat, tmp_surface->w, tmp_surface->h, 0, GL_BGR, // => SDL_LoadBMP GL_UNSIGNED_BYTE, tmp_surface->pixels); SDL_FreeSurface(tmp_surface); } model read_obj_file(char* filename) { model mdl; char buffer[100]; FILE *obj_file = NULL; int v_count = 0; int vn_count = 0; int vt_count = 0; int f_count = 0; obj_file = fopen(filename, "r"); if (obj_file == NULL) while (fgets(buffer, 100, obj_file)) { /* puts(buffer); */ /* vertex coords */ if (buffer[0] == 'v' && buffer [1] == ' ') { v_count++; sscanf(buffer, "v %f %f %f", &mdl.vertexes[v_count].x, &mdl.vertexes[v_count].y, &mdl.vertexes[v_count].z); #if RESIZE mdl.vertexes[v_count].x *= resize_coefficient; mdl.vertexes[v_count].y *= resize_coefficient; mdl.vertexes[v_count].z *= resize_coefficient; #endif } /* vertex normals */ else if (buffer[0] == 'v' && buffer[1] == 'n') { vn_count++; sscanf(buffer, "vn %f %f %f", &mdl.normals[v_count].x, &mdl.normals[v_count].y, &mdl.normals[v_count].z); } /* texture coords */ else if (buffer[0] == 'v' && buffer[1] == 't') { vt_count++; sscanf(buffer, "vt %f %f", &mdl.text_coords[vt_count].v, &mdl.text_coords[vt_count].u); } /* faces */ else if (buffer[0] == 'f' && buffer [1] == ' ') { int slash_count = 0; int dd = 0; f_count++; for (dd = 2; buffer[dd] != ' '; dd++) if (buffer[dd] == '/') slash_count++; /* printf("%i\n", slash_count); */ if (slash_count == 1) sscanf(buffer, "f %i/%i %i/%i %i/%i", &mdl.faces[f_count].v1, &mdl.faces[f_count].vt1, &mdl.faces[f_count].v2, &mdl.faces[f_count].vt2, &mdl.faces[f_count].v3, &mdl.faces[f_count].vt3 ); else if (slash_count == 2) sscanf(buffer, "f %i/%i/%i %i/%i/%i %i/%i/%i", &mdl.faces[f_count].v1, &mdl.faces[f_count].vt1, &mdl.faces[f_count].vn1, &mdl.faces[f_count].v2, &mdl.faces[f_count].vt2, &mdl.faces[f_count].vn2, &mdl.faces[f_count].v3, &mdl.faces[f_count].vt3, &mdl.faces[f_count].vn3); else { puts("ERRRRORRRR!!!"); exit(0); } } } fclose(obj_file); #if AFTERSCAN_PRINT #endif mdl.f_count = f_count; //remember for each model. later we'll use it for rendering return mdl; } void draw_model(/*int texnum, */model mdl) { int ii = 0; //glBindTexture(GL_TEXTURE_2D, texture[texnum]); /* Select Texture */ glBindTexture(GL_TEXTURE_2D, texture); /* Select Texture */ glBegin(GL_TRIANGLES); { for (ii = 0; ii <= mdl.f_count; ii++) { /* TODO: remake whole structure */ glTexCoord2f(mdl.text_coords[ mdl.faces[ii].vt1 ].u, mdl.text_coords[ mdl.faces[ii].vt1 ].v); glVertex3f(mdl.vertexes [ mdl.faces[ii].v1 ].x, mdl.vertexes [ mdl.faces[ii].v1 ].y, mdl.vertexes [ mdl.faces[ii].v1 ].z); //glNormal3f(mdl.normals [ mdl.faces[ii].v1 ].x, // mdl.normals [ mdl.faces[ii].v1 ].y, // mdl.normals [ mdl.faces[ii].v1 ].z); glTexCoord2f(mdl.text_coords[ mdl.faces[ii].vt2 ].u, mdl.text_coords[ mdl.faces[ii].vt2 ].v); glVertex3f(mdl.vertexes [ mdl.faces[ii].v2 ].x, mdl.vertexes [ mdl.faces[ii].v2 ].y, mdl.vertexes [ mdl.faces[ii].v2 ].z); //glNormal3f(mdl.normals [ mdl.faces[ii].v1 ].x, // mdl.normals [ mdl.faces[ii].v1 ].y, // mdl.normals [ mdl.faces[ii].v1 ].z); glTexCoord2f(mdl.text_coords[ mdl.faces[ii].vt3 ].u, mdl.text_coords[ mdl.faces[ii].vt3 ].v); glVertex3f(mdl.vertexes [ mdl.faces[ii].v3 ].x, mdl.vertexes [ mdl.faces[ii].v3 ].y, mdl.vertexes [ mdl.faces[ii].v3 ].z); //glNormal3f(mdl.normals [ mdl.faces[ii].v1 ].x, // mdl.normals [ mdl.faces[ii].v1 ].y, // mdl.normals [ mdl.faces[ii].v1 ].z); } } glEnd(); } void reshape(int w, int h) { glViewport(0, 0, w, h); // Reset The Current Viewport glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45, (GLfloat)w/(GLfloat)h, 1, 100); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void draw_filled_hex() { glBegin(GL_POLYGON); { glNormal3f(0, 0, 1); glVertex2f(0, HEX_EX); glVertex2f(-HEX_IN, +HEX_IN/2); glVertex2f(-HEX_IN, -HEX_IN/2); glVertex2f(0, -HEX_EX); glVertex2f(HEX_IN, -HEX_IN/2); glVertex2f(HEX_IN, HEX_IN/2); } glEnd(); } void draw_wire_hex() { #if(0) glLineStipple(1,0x00FF); glEnable(GL_LINE_STIPPLE); #endif glBegin(GL_LINE_STRIP); { glVertex2f(0, HEX_EX); glVertex2f(-HEX_IN, +HEX_IN/2); glVertex2f(-HEX_IN, -HEX_IN/2); glVertex2f(0, -HEX_EX); glVertex2f(HEX_IN, -HEX_IN/2); glVertex2f(HEX_IN, HEX_IN/2); } glEnd(); glDisable(GL_LINE_STIPPLE); } void draw_character(int x, int y, character *ch) { if ((x >= 0 && x < x_count) && (y >= 0 && y < y_count)) { glPushMatrix(); { translate(y, x); { glTranslatef(0, 0, 0.01); //slightly above the floor //player identifier if (ch->player == 1) glColor3f(1.0, 0.5, 0.5); if (ch->player == 2) glColor3f(0.5, 0.5, 1.0); glDisable(GL_TEXTURE_2D); gluDisk(gliquadric, HEX_IN*0.7, HEX_IN*0.9, 12, 2); glColor3f(0.3, 1, 0.3); gluPartialDisk(gliquadric, HEX_IN*0.5, HEX_IN*0.7, ch->ap, 2, 0, ch->ap*60.0); } #if(0) //draw test glColor3f(1, 1, 1); glTranslatef(0, 0, 0.2); //slightly above the floor gluSphere(gliquadric, HEX_IN*0.3, 6, 6); #else //draw model glEnable(GL_TEXTURE_2D); // glEnable(GL_LIGHTING); glColor3f(1, 1, 1); glRotatef(90, 1, 0, 0); // because of blender exporter glRotatef(ch->rot, 0, 1, 0); //individual character rotation //draw_model(mdl[ch->frame]); draw_model(mdl_static); #if(1) //shadow glDisable(GL_TEXTURE_2D); glScalef(1, 0.01, 1); glColor4f(0, 0, 0, 0.5); draw_model(mdl_static); glEnable(GL_TEXTURE_2D); #endif #endif } glPopMatrix(); } } #define PICK_BG 0 #define PICK_HEX 1 void draw_for_picking() { glDisable(GL_TEXTURE_2D); // //glDisable(GL_LIGHTING); int x, y; glClearColor(0, 0, PICK_BG, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //position { glTranslatef(0, 0, -6); // Translate 6 Units Into The Screen glRotatef(-ROTATE_ANGLE_2, 1, 0, 0); glRotatef(ROTATE_ANGLE, 0, 0, 1); glTranslatef(camera_x, camera_y, 0); } glPushMatrix(); { for (y=0; y<y_count; y++) { for (x=0; x<x_count; x++) { glColor3ub(x, y, PICK_HEX); draw_filled_hex(); glTranslatef(2*HEX_IN, 0, 0); } if (y%2 == 0) glTranslatef(-HEX_IN, 1.5*HEX_EX, 0); else glTranslatef(+HEX_IN, 1.5*HEX_EX, 0); glTranslatef(-(2*HEX_IN *x_count), 0, 0); } } glPopMatrix(); // glEnable(GL_LIGHTING); } void draw() { glDisable(GL_TEXTURE_2D); //glDisable(GL_LIGHTING); //glShadeModel(GL_SMOOTH); int x, y; glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //position { glTranslatef(0, 0, -6); // Translate 6 Units Into The Screen glRotatef(-ROTATE_ANGLE_2, 1, 0, 0); //glRotatef(-60, 1, 0, 0); glRotatef(ROTATE_ANGLE, 0, 0, 1); glTranslatef(camera_x, camera_y, 0); } glPushMatrix(); for (y=0; y< y_count; y++) { for (x=0; x<x_count; x++) { glLineWidth(5); if (IS_SOMEONE_SELECTED && sX == x && sY == y) { glPushMatrix(); glColor3f(0.8, 0.3, 0.3); glTranslatef(0, 0, 0.01); gluDisk(gliquadric, HEX_IN*0.9, HEX_IN*0.95, 12, 2); glPopMatrix(); } if (PICKED_SMTH && aX == x && aY == y) { glPushMatrix(); glColor3f(0.3, 0.8, 0.3); glTranslatef(0, 0, 0.01); gluDisk(gliquadric, HEX_IN*0.95, HEX_IN, 12, 2); glPopMatrix(); } if (IS_SOMEONE_SELECTED) { if (MAP[sX][sY].ch != NULL && MAP[x][y].move_cost <= MAP[sX][sY].ch->ap) { glPushMatrix(); glColor3f(0.5, 0.5, 1.0); glTranslatef(0, 0, 0.01); gluDisk(gliquadric, HEX_IN*0.2, HEX_IN*0.4, 12, 2); glPopMatrix(); } } float coff = 1.0 - (float)(MAP[x][y].init_cost*20)/256; glColor3f(coff, coff, coff); draw_filled_hex(); #if(1) //draw wire #if(0) //color it as current player if (PLAYER == 1) glColor3f(1.0, 0.5, 0.5); else glColor3f(0.5, 0.5, 1.0); #else //gray wire glColor3f(0.5, 0.5, 0.5); #endif glLineWidth(3); draw_wire_hex(); #endif //draw wire glTranslatef(2*HEX_IN, 0, 0); } if (y%2 == 0) glTranslatef(-HEX_IN, 1.5*HEX_EX, 0); else glTranslatef(+HEX_IN, 1.5*HEX_EX, 0); glTranslatef(-(2*HEX_IN *x_count), 0, 0); } glPopMatrix(); glEnable(GL_TEXTURE_2D); //glEnable(GL_LIGHTING); for (x=0; x<x_count; x++) { for (y=0; y<y_count; y++) { if (MAP[x][y].ch != NULL) draw_character(x, y, MAP[x][y].ch); } } } void do_picking() { GLint viewport[4]; GLubyte pixel[3]; glGetIntegerv(GL_VIEWPORT, viewport); glReadPixels(cursorX, viewport[3]-cursorY, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, (void*)pixel); aX = pixel[0]; aY = pixel[1]; //-------------------------- PICKED_SMTH = (bool)pixel[2]; if (pixel[2] > 1) { //printf("some button\n"); } } void fill_map(Q *q, ushort by, ushort bx) { ushort x, y; for (y=0; y<y_count; y++) { for (x=0; x<x_count; x++) { MAP[x][y].move_cost = 30000; } } push(q, bx, by, bx, by, 0); //start point while (pop(q, &x, &y)) { try_push(q, x+1, y , x, y); try_push(q, x-1, y , x, y); try_push(q, x , y+1, x, y); try_push(q, x , y-1, x, y); if (y%2==0) { try_push(q, x+1, y-1, x, y); try_push(q, x+1, y+1, x, y); } else { try_push(q, x-1, y-1, x, y); try_push(q, x-1, y+1, x, y); } } } ushort back_path_x[100]; ushort back_path_y[100]; ushort back_path_current = 0; //TODO void get_path(ushort by, ushort bx, ushort ey, ushort ex) { back_path_current = 0; ushort x = ex, y = ey, tmpx, tmpy; while ((x != bx) || (y != by)) { back_path_x[back_path_current] = x; back_path_y[back_path_current] = y; back_path_current++; tmpx = MAP[x][y].parent_x; tmpy = MAP[x][y].parent_y; x = tmpx; y = tmpy; } back_path_x[back_path_current] = x; back_path_y[back_path_current] = y; back_path_current++; } void end_player_turn() { IS_SOMEONE_SELECTED = false; if (PLAYER == 1) PLAYER = 2; else PLAYER = 1; int y, x; for (y=0; y<y_count; y++) { for (x=0; x<x_count; x++) { if (MAP[x][y].ch != NULL && MAP[x][y].ch->player == PLAYER) { MAP[x][y].ch->ap = 6; } } } } void handle_mouse_events(SDL_Event E) { if (PICKED_SMTH && MODE == MODE_SELECT) { if (MAP[aX][aY].ch != NULL) { if (MAP[aX][aY].ch->player == PLAYER) { // select character sX = aX; sY = aY; IS_SOMEONE_SELECTED = true; fill_map(&q, sY, sX); } else { if (IS_SOMEONE_SELECTED && MAP[sX][sY].ch->ap >= (3 + MAP[aX][aY].move_cost)) { // try to kill KILL enemy unit! if (rand()%2) { free(MAP[aX][aY].ch); MAP[aX][aY].ch = NULL; } MAP[sX][sY].ch->ap -= 3 + MAP[aX][aY].move_cost; } } } else { if (IS_SOMEONE_SELECTED && MAP[sX][sY].ch != NULL && MAP[sX][sY].ch->ap > 0) { // move selected character fill_map(&q, sY, sX); if (MAP[aX][aY].move_cost <= MAP[sX][sY].ch->ap) { //if character has needed action points MAP[sX][sY].ch->ap -= MAP[aX][aY].move_cost; TMP_CHAR = MAP[sX][sY].ch; MAP[sX][sY].ch = NULL; get_path(sY, sX, aY, aX); //START_MOVE(sX, sY, aX, aY); MODE = MODE_MOVE; } } } } } bool IS_CAMERA_ROTATING = false; void hanlde_events(SDL_Event E) { switch (E.type) { default: //puts("unknown event!"); break; case SDL_QUIT: isProgramLooping = false; break; case SDL_VIDEORESIZE: reshape(E.resize.w, E.resize.h); break; case SDL_MOUSEMOTION: cursorX = E.motion.x; cursorY = E.motion.y; if(IS_CAMERA_ROTATING ){ ROTATE_ANGLE += E.motion.xrel; ROTATE_ANGLE_2 += E.motion.yrel; } break; case SDL_MOUSEBUTTONDOWN: if(E.button.button == SDL_BUTTON_RIGHT){ IS_CAMERA_ROTATING = true; } handle_mouse_events(E); break; case SDL_MOUSEBUTTONUP: if(E.button.button == SDL_BUTTON_RIGHT){ IS_CAMERA_ROTATING = false; } handle_mouse_events(E); break; case SDL_KEYDOWN: switch (E.key.keysym.sym) { default: break; case SDLK_q: case SDLK_ESCAPE: isProgramLooping = false; break; case SDLK_SPACE: end_player_turn(); break; case SDLK_UP: camera_y--; break; case SDLK_DOWN: camera_y++; break; case SDLK_LEFT: camera_x++; break; case SDLK_RIGHT: camera_x--; break; } } } void init_sdl() { SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); atexit(SDL_Quit); /* the flags to pass to SDL_SetVideoMode */ video_flags = SDL_OPENGL; video_flags |= SDL_GL_DOUBLEBUFFER; video_flags |= SDL_RESIZABLE; SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); /* get a SDL surface */ surface = SDL_SetVideoMode(SCR_WIDTH, SCR_HEIGHT, SCR_BPP, video_flags); SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, 0, 0, 0, 0)); reshape(SCR_WIDTH, SCR_HEIGHT); } void init_opengl() { glEnable(GL_TEXTURE_2D); glShadeModel(GL_SMOOTH); glClearColor(0, 0, 0, 1); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); //glEnable(GL_LIGHTING); glEnable(GL_AUTO_NORMAL); glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); float POS[] = {0, 1, 0, 0}; glLightfv(GL_LIGHT0, GL_POSITION, POS); glDepthFunc(GL_LEQUAL); /* The Type Of Depth Test To Do */ /* Really Nice Perspective Calculations */ glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); glLineWidth(4); //glEnable(GL_CULL_FACE); //glFrontFace(GL_CW); //glCullFace(GL_BACK); //glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); } bool create_char(int x, int y, int player) { MAP[y][x].ch = malloc(sizeof(character)); character *ch = malloc(sizeof(character)); ch->player = player; ch->rot = (rand()%6)*60+30; ch->frame = 0; ch->ap = 6; MAP[y][x].ch = ch; return(true); } void init_map() { int i, j; for (i=0; i<y_count; i++) { for (j=0; j<x_count; j++) { MAP[i][j].ch = NULL; MAP[i][j].init_cost = (rand()%2)*3+1; MAP[i][j].move_cost = 30000; if (rand()%10 > 7) create_char(j, i, rand()%2+1); } } } void move_logic() { if (SPECIAL_STEPS > 0) { // we have not reached the destination SPECIAL_X += SPECIAL_STEP_X; SPECIAL_Y += SPECIAL_STEP_Y; SPECIAL_STEPS--; } else { if (back_path_current > 1) { back_path_current--; START_MOVE(back_path_x[back_path_current], back_path_y[back_path_current], back_path_x[back_path_current-1], back_path_y[back_path_current-1] ); } else { // END MODE = MODE_SELECT; MAP[ex][ey].ch = TMP_CHAR; sX = ex; sY = ey; fill_map(&q, sY, sX); } } } void move_draw() { glTranslatef(SPECIAL_X, SPECIAL_Y, 0); //glTranslatef(0, 0, 0.2); //slightly above the floor glRotatef(90, 1, 0, 0); // because of blender exporter if (SPECIAL_STEP_Y <= 0) glRotatef(SPECIAL_ANGLE, 0, 1, 0); else glRotatef(SPECIAL_ANGLE, 0, -1, 0); glColor3f(1, 1, 1); #if(0) //test glTranslatef(0, 0, 0.2); //slightly above the floor gluSphere(gliquadric, HEX_IN*0.3, 6, 6); #else //draw character draw_model(mdl[FRAME]); #if(1) //shadow glScalef(1, 0.01, 1); glColor4f(0, 0, 0, 0.5); draw_model(mdl[FRAME]); #endif //~shadow #endif //~test } unsigned generate_texture_from_str(char* Text, int *w, int *h) { //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); SDL_Color Color = {255, 255, 255}; SDL_Surface *txt = TTF_RenderUTF8_Blended(font, Text, Color); unsigned Texture = 0; glGenTextures(1, &Texture); /*Generate an OpenGL 2D texture from the SDL_Surface*.*/ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, txt->w, txt->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, txt->pixels); *w = txt->w; *h = txt->h; SDL_FreeSurface(txt); return(Texture); } bool add_button(int x, int y, int W, int H, char* name, char *text) { unsigned textureID; int w, h; textureID = generate_texture_from_str(text, &w, &h); if (textureID == 0) exit(1); gui_button b = {x, y, W, H, name, textureID, w, h}; BUTTONS[current_button] = b; current_button++; return(true); } void draw_button(gui_button b) { glPushMatrix(); { glTranslatef(b.x, b.y, 0); glColor4f(0.3, 0.3, 0.3, 0.8); glBegin(GL_QUADS); { glVertex3d(0, 0, 0); glVertex3d(b.sx, 0, 0); glVertex3d(b.sx, b.sy, 0); glVertex3d(0, b.sy, 0); } glEnd(); glColor4f(1, 1, 1, 0.8); glScalef(0.3, 0.3, 0.3); //RENDER TEXT { glEnable(GL_TEXTURE_2D); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBindTexture(GL_TEXTURE_2D, b.textureID); //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glBegin(GL_QUADS); { glTexCoord2d(0, 0); glVertex2d(0, 0); glTexCoord2d(1, 0); glVertex2d(b.textSX, 0); glTexCoord2d(1, 1); glVertex2d(b.textSX, b.textSY); glTexCoord2d(0, 1); glVertex2d(0, b.textSY); } glEnd(); glDisable(GL_TEXTURE_2D); } } glPopMatrix(); } void draw_button_for_picking(gui_button b) { glPushMatrix(); { glTranslatef(b.x, b.y, 0); glBegin(GL_QUADS); { glVertex3d(0, 0, 0); glVertex3d(b.sx, 0, 0); glVertex3d(b.sx, b.sy, 0); glVertex3d(0, b.sy, 0); } glEnd(); } glPopMatrix(); } void draw_interface() { glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0, SCR_WIDTH, SCR_HEIGHT, 0, 0, 1); glMatrixMode(GL_MODELVIEW); glPushMatrix(); { glLoadIdentity(); int i; for (i = 0; i < current_button; i++) draw_button(BUTTONS[i]); } glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glEnable(GL_DEPTH_TEST); glDisable(GL_TEXTURE_2D); glDisable(GL_BLEND); } void draw_interface_for_picking() { glDisable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0, SCR_WIDTH, SCR_HEIGHT, 0, 0, 1); glMatrixMode(GL_MODELVIEW); glPushMatrix(); { glLoadIdentity(); int i; for (i = 0; i < current_button; i++) { glColor3ub(0, 0, i+2); draw_button_for_picking(BUTTONS[i]); } } glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glEnable(GL_DEPTH_TEST); } void init_interface() { if (TTF_Init() == -1) { exit(1); } font = TTF_OpenFont("font.ttf", 60); add_button(0, 0, 80, 25, "player identifier", "красный"); add_button(300, 300, 80, 25, "test button 1", "butt 1"); add_button(350, 350, 80, 25, "test button 2", "butt 2"); } #undef main //write straight to the cosole int main(void) { gliquadric = gluNewQuadric(); SDL_Event E; Uint32 next_time; srand(time(0)); if (init_q(&q, 66000) == false) { puts("can't init Qu!"); return(1); } //load_texture("reegie1.3.1__RIFF_1.bmp"); mdl_static = read_obj_file("test2/reegie1.3.1__RIFF_3_000010.obj"); int i; for (i = 0; i < 32; i++) { char obj_file_name[100]; sprintf(obj_file_name, "test2/reegie1.3.1__RIFF_3_0000%02i.obj", i+1); mdl[i] = read_obj_file(obj_file_name); } init_sdl(); init_opengl(); init_interface(); init_map(); HEX_EX = 0.4f; HEX_IN = sqrt(HEX_EX*HEX_EX - (HEX_EX/2)*(HEX_EX/2)); //printf("%i\n", __LINE__); next_time = SDL_GetTicks() + 300; while (isProgramLooping) { //HANDLE EVENTS { while (SDL_PollEvent(&E)) hanlde_events(E); } //PICK { draw_for_picking(); draw_interface_for_picking(font); do_picking(); } //DRAW { draw(); if (MODE == MODE_MOVE) move_draw(); //if(MODE == MODE_ATACK) // atack(); //if(MODE == MODE_ROTATE) // rotate(); draw_interface(font); SDL_GL_SwapBuffers(); } //LOGIC { if (MODE == MODE_MOVE) move_logic(); } //TIME/FPS { if (next_time > SDL_GetTicks()) //SDL_Delay(next_time - SDL_GetTicks()); ; else next_time += 20; //SHIT FRAME++; if (FRAME == 32) FRAME = 0; next_time += 20; } } return 0; }
You need to login to post a comment.
