Posted By

ozkriff on 02/23/10


Tagged

opengl SDL


Versions (?)

again(


 / Published in: C
 

  1. //#include "GL/glew.h"
  2. #include "SDL/SDL.h"
  3. #include "SDL/SDL_ttf.h"
  4. #include "SDL/SDL_opengl.h"
  5. #include <math.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdbool.h>
  10. #include <time.h>
  11.  
  12. //#define M_PI 3.1415296
  13.  
  14. // astyle -SUET81
  15.  
  16. /* LINUX
  17. gcc main.c -Wall -g -o hex -lGL -lGLU -lSDL
  18. */
  19.  
  20. /* WIN
  21. @echo off
  22. PATH = ..\..\b\MinGW\bin;
  23. set libs=-lmingw32 -lglew32 -lSDLmain -lSDL -lSDL_image -lSDL_ttf -lopengl32 -lglu32
  24. set IN=main.c
  25. set OUT=hex.exe
  26. @echo on
  27. gcc.exe %IN% -pg -o %OUT% %libs% && hex.exe
  28. */
  29.  
  30. GLUquadricObj *gliquadric;
  31. float camera_x=0, camera_y=0;
  32.  
  33. #define MODE_SELECT 1
  34. #define MODE_MOVE 2
  35. #define MODE_ATACK 3
  36. #define MODE_PAUSE 4
  37. int MODE = MODE_SELECT;
  38.  
  39. int PLAYER = 1;
  40.  
  41. int cursorX, cursorY;
  42. int aX = 99, aY = 99; // active cell
  43. int sX = 99, sY = 99; //selected cell
  44. bool IS_SOMEONE_SELECTED = false;
  45.  
  46. float ROTATE_ANGLE = 0;
  47. float ROTATE_ANGLE_2 = 60;
  48. float HEX_EX;
  49. float HEX_IN;
  50. #define x_count (6)
  51. #define y_count (6)
  52.  
  53. #define ushort unsigned short int
  54. #define uint unsigned int
  55.  
  56.  
  57. int PICKED_SMTH;
  58. bool isProgramLooping = true;
  59. SDL_Surface *surface;
  60. int video_flags; /* Flags to pass to SDL_SetVideoMode */
  61.  
  62. int FRAME = 0;
  63. int SCR_WIDTH = 640;
  64. int SCR_HEIGHT = 480;
  65. int SCR_BPP = 32;
  66.  
  67. #define AFTERSCAN_PRINT 0
  68. #define ROT_BY_KEYS 0
  69. #define NO_FILL 0
  70. #define RESIZE 1
  71. #define resize_coefficient (0.20)
  72.  
  73. #define V_COUNT 3000
  74. #define VT_COUNT 3000
  75. #define VN_COUNT 3000
  76. #define F_COUNT 3000
  77.  
  78. TTF_Font* font;
  79.  
  80. typedef struct {
  81. float x, y, z;
  82. } vector;
  83.  
  84. typedef struct {
  85. float u, v;
  86. } texture_coords;
  87.  
  88. typedef struct {
  89. int v1, v2, v3,
  90. vt1, vt2, vt3,
  91. vn1, vn2, vn3;
  92. } indexes;
  93.  
  94. typedef struct {
  95. vector vertexes[V_COUNT ];
  96. vector normals[VN_COUNT];
  97. texture_coords text_coords[VT_COUNT];
  98. indexes faces[F_COUNT ];
  99.  
  100. int f_count;
  101. } model;
  102.  
  103. typedef struct {
  104. //int x, y; // map position
  105. int health;
  106. int player;
  107. float rot;
  108. int frame;
  109. //bool can_atack;
  110. //bool can_move;
  111. ushort ap; //acton points
  112. } character;
  113.  
  114.  
  115. typedef struct cell cell;
  116. struct cell {
  117. uint init_cost;
  118. uint move_cost;
  119. ushort parent_x, parent_y;
  120. character *ch;
  121. };
  122. cell MAP[x_count][y_count];
  123.  
  124.  
  125.  
  126.  
  127. model mdl[32];
  128. model mdl_static;
  129. GLuint texture;
  130.  
  131. int bx;
  132. int by;
  133. int ex;
  134. int ey;
  135.  
  136. float rbx;
  137. float rby;
  138. float rex;
  139. float rey;
  140.  
  141. float SPECIAL_X;
  142. float SPECIAL_Y;
  143.  
  144. float SPECIAL_SPEED;
  145. float GIPATINUZA;
  146. float SPECIAL_ANGLE;
  147. float SPECIAL_STEP_X;
  148. float SPECIAL_STEP_Y;
  149. int SPECIAL_STEPS;
  150. character *TMP_CHAR;
  151.  
  152.  
  153.  
  154. typedef struct {
  155. int x, y; //position
  156. int sx, sy; //size
  157. char *name;
  158. unsigned textureID;
  159. int textSX, textSY;
  160. //colorFG
  161. //colorBG
  162. } gui_button;
  163. int current_button = 0;
  164. gui_button BUTTONS[100];
  165.  
  166.  
  167. void
  168. map_to_real(int mx, int my, float *rx, float *ry)
  169. {
  170. *rx = (2 * HEX_IN * mx);
  171. if (my%2 != 0)
  172. *rx -= HEX_IN;
  173. *ry = 1.5 * HEX_EX * my;
  174. }
  175.  
  176.  
  177. void
  178. translate(ushort y, ushort x)
  179. {
  180. float ry, rx;
  181. map_to_real(x, y, &rx, &ry);
  182.  
  183. glTranslatef(rx, ry, 0);
  184. }
  185.  
  186.  
  187.  
  188. void
  189. START_MOVE(int bX, int bY, int eX, int eY)
  190. {
  191. bx = bX;
  192. by = bY;
  193. ex = eX;
  194. ey = eY;
  195.  
  196. map_to_real(bx, by, &rbx, &rby);
  197. map_to_real(ex, ey, &rex, &rey);
  198.  
  199. SPECIAL_X = rbx;
  200. SPECIAL_Y = rby;
  201.  
  202. SPECIAL_SPEED = 0.031;
  203. GIPATINUZA = sqrt(pow(rey-rby, 2) + pow(rex-rbx, 2));
  204.  
  205. SPECIAL_ANGLE = (asin((rex-rbx)/GIPATINUZA))/ M_PI * 180.0;
  206. if (ey-by > 0)
  207. SPECIAL_ANGLE += 180;
  208.  
  209. SPECIAL_STEP_X = SPECIAL_SPEED * ((rex-rbx)/GIPATINUZA);
  210. SPECIAL_STEP_Y = SPECIAL_SPEED * ((rey-rby)/GIPATINUZA);
  211. SPECIAL_STEPS = (int)(GIPATINUZA/SPECIAL_SPEED);
  212. }
  213.  
  214.  
  215.  
  216. typedef struct Q Q;
  217. struct Q {
  218. long int tail;
  219. long int head;
  220. ushort *qx;
  221. ushort *qy;
  222. long int qu_size;
  223. };
  224. Q q;
  225.  
  226.  
  227.  
  228. void
  229. push(Q *q, ushort x, ushort y, ushort px, ushort py, uint NEW)
  230. {
  231. MAP[x][y].move_cost = NEW;
  232. MAP[x][y].parent_x = px;
  233. MAP[x][y].parent_y = py;
  234.  
  235. if (q->tail == q->qu_size) {
  236. q->tail = 0;
  237. }
  238. q->qx[q->tail] = x;
  239. q->qy[q->tail] = y;
  240. (q->tail)++;
  241. }
  242.  
  243.  
  244.  
  245. void
  246. try_push(Q *q, ushort x, ushort y, ushort px, ushort py)
  247. {
  248. if ((x < x_count) && (y < y_count)) { //(x >= 0 && y >= 0) but ushort
  249. uint NEW = MAP[px][py].move_cost + MAP[x][y].init_cost;
  250. if ((MAP[x][y].move_cost > NEW)) //if(OLD > NEW)
  251. push(q, x, y, px, py, NEW);
  252. }
  253. }
  254.  
  255.  
  256.  
  257. bool
  258. pop(Q *q, ushort *x, ushort *y)
  259. {
  260. if (q->head == q->tail) {
  261. return(false); // Q is empty
  262. }
  263.  
  264. *x = q->qx[q->head];
  265. *y = q->qy[q->head];
  266. (q->head)++;
  267. if (q->head == q->qu_size)
  268. q->head = 0;
  269.  
  270. return(true);
  271. }
  272.  
  273.  
  274.  
  275. bool
  276. init_q(Q *q, long int size)
  277. {
  278. q->qx = (malloc(size * sizeof(ushort)));
  279. q->qy = (malloc(size * sizeof(ushort)));
  280.  
  281. if (q->qx == NULL || q->qy == NULL)
  282. return(false);
  283.  
  284. q->tail = 0;
  285. q->head = 0;
  286. q->qu_size = size;
  287.  
  288. return(true);
  289. }
  290.  
  291.  
  292.  
  293. void
  294. load_texture(/*int texnum, */char *name)
  295. {
  296. SDL_Surface *tmp_surface;
  297. // GLenum textureFormat;
  298.  
  299. //tmp_surface = IMG_Load(name);
  300. tmp_surface = SDL_LoadBMP(name);
  301.  
  302. #if 0
  303. if (tmp_surface->format->BytesPerPixel == 4) {
  304. if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  305. textureFormat = GL_BGRA;
  306. else
  307. textureFormat = GL_RGBA;
  308. } else {
  309. if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  310. textureFormat = GL_BGR;
  311. else
  312. textureFormat = GL_RGB;
  313. }
  314. #endif
  315.  
  316. /* Genarate texture */
  317. //glGenTextures(1, &texture[texnum]);
  318. glGenTextures(1, &texture);
  319. //glBindTexture(GL_TEXTURE_2D, texture[texnum]);
  320. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  321. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  322. glTexImage2D(GL_TEXTURE_2D, 0, tmp_surface->format->BytesPerPixel,
  323. //tmp_surface->w, tmp_surface->h, 0, textureFormat,
  324. tmp_surface->w, tmp_surface->h, 0, GL_BGR, // => SDL_LoadBMP
  325. GL_UNSIGNED_BYTE, tmp_surface->pixels);
  326.  
  327. SDL_FreeSurface(tmp_surface);
  328. }
  329.  
  330.  
  331.  
  332. model
  333. read_obj_file(char* filename)
  334. {
  335. model mdl;
  336. char buffer[100];
  337. FILE *obj_file = NULL;
  338.  
  339. int v_count = 0;
  340. int vn_count = 0;
  341. int vt_count = 0;
  342. int f_count = 0;
  343.  
  344.  
  345. obj_file = fopen(filename, "r");
  346. if (obj_file == NULL)
  347. printf("can't find file: %s", filename);
  348.  
  349. while (fgets(buffer, 100, obj_file)) {
  350. /* puts(buffer); */
  351. /* vertex coords */
  352. if (buffer[0] == 'v' && buffer [1] == ' ') {
  353. v_count++;
  354. sscanf(buffer, "v %f %f %f",
  355. &mdl.vertexes[v_count].x,
  356. &mdl.vertexes[v_count].y,
  357. &mdl.vertexes[v_count].z);
  358. #if RESIZE
  359. mdl.vertexes[v_count].x *= resize_coefficient;
  360. mdl.vertexes[v_count].y *= resize_coefficient;
  361. mdl.vertexes[v_count].z *= resize_coefficient;
  362. #endif
  363. }
  364.  
  365. /* vertex normals */
  366. else if (buffer[0] == 'v' && buffer[1] == 'n') {
  367. vn_count++;
  368. sscanf(buffer, "vn %f %f %f",
  369. &mdl.normals[v_count].x,
  370. &mdl.normals[v_count].y,
  371. &mdl.normals[v_count].z);
  372. }
  373.  
  374. /* texture coords */
  375. else if (buffer[0] == 'v' && buffer[1] == 't') {
  376. vt_count++;
  377. sscanf(buffer, "vt %f %f",
  378. &mdl.text_coords[vt_count].v,
  379. &mdl.text_coords[vt_count].u);
  380. }
  381.  
  382. /* faces */
  383. else if (buffer[0] == 'f' && buffer [1] == ' ') {
  384. int slash_count = 0;
  385. int dd = 0;
  386. f_count++;
  387. for (dd = 2; buffer[dd] != ' '; dd++)
  388. if (buffer[dd] == '/')
  389. slash_count++;
  390. /* printf("%i\n", slash_count); */
  391.  
  392. if (slash_count == 1)
  393. sscanf(buffer, "f %i/%i %i/%i %i/%i",
  394. &mdl.faces[f_count].v1, &mdl.faces[f_count].vt1,
  395. &mdl.faces[f_count].v2, &mdl.faces[f_count].vt2,
  396. &mdl.faces[f_count].v3, &mdl.faces[f_count].vt3
  397. );
  398. else if (slash_count == 2)
  399. sscanf(buffer, "f %i/%i/%i %i/%i/%i %i/%i/%i",
  400. &mdl.faces[f_count].v1, &mdl.faces[f_count].vt1, &mdl.faces[f_count].vn1,
  401. &mdl.faces[f_count].v2, &mdl.faces[f_count].vt2, &mdl.faces[f_count].vn2,
  402. &mdl.faces[f_count].v3, &mdl.faces[f_count].vt3, &mdl.faces[f_count].vn3);
  403. else {
  404. puts("ERRRRORRRR!!!");
  405. exit(0);
  406. }
  407.  
  408. }
  409. }
  410.  
  411. fclose(obj_file);
  412.  
  413. #if AFTERSCAN_PRINT
  414. printf("vertex count: %i\n", v_count);
  415. printf("texture coords count: %i\n", vt_count);
  416. printf("faces count: %i\n", f_count);
  417. #endif
  418.  
  419. mdl.f_count = f_count; //remember for each model. later we'll use it for rendering
  420.  
  421. return mdl;
  422. }
  423.  
  424.  
  425.  
  426. void
  427. draw_model(/*int texnum, */model mdl)
  428. {
  429. int ii = 0;
  430.  
  431. //glBindTexture(GL_TEXTURE_2D, texture[texnum]); /* Select Texture */
  432. glBindTexture(GL_TEXTURE_2D, texture); /* Select Texture */
  433.  
  434. glBegin(GL_TRIANGLES);
  435. {
  436. for (ii = 0; ii <= mdl.f_count; ii++) {
  437. /* TODO: remake whole structure */
  438. glTexCoord2f(mdl.text_coords[ mdl.faces[ii].vt1 ].u,
  439. mdl.text_coords[ mdl.faces[ii].vt1 ].v);
  440. glVertex3f(mdl.vertexes [ mdl.faces[ii].v1 ].x,
  441. mdl.vertexes [ mdl.faces[ii].v1 ].y,
  442. mdl.vertexes [ mdl.faces[ii].v1 ].z);
  443. //glNormal3f(mdl.normals [ mdl.faces[ii].v1 ].x,
  444. // mdl.normals [ mdl.faces[ii].v1 ].y,
  445. // mdl.normals [ mdl.faces[ii].v1 ].z);
  446.  
  447. glTexCoord2f(mdl.text_coords[ mdl.faces[ii].vt2 ].u,
  448. mdl.text_coords[ mdl.faces[ii].vt2 ].v);
  449. glVertex3f(mdl.vertexes [ mdl.faces[ii].v2 ].x,
  450. mdl.vertexes [ mdl.faces[ii].v2 ].y,
  451. mdl.vertexes [ mdl.faces[ii].v2 ].z);
  452. //glNormal3f(mdl.normals [ mdl.faces[ii].v1 ].x,
  453. // mdl.normals [ mdl.faces[ii].v1 ].y,
  454. // mdl.normals [ mdl.faces[ii].v1 ].z);
  455.  
  456. glTexCoord2f(mdl.text_coords[ mdl.faces[ii].vt3 ].u,
  457. mdl.text_coords[ mdl.faces[ii].vt3 ].v);
  458. glVertex3f(mdl.vertexes [ mdl.faces[ii].v3 ].x,
  459. mdl.vertexes [ mdl.faces[ii].v3 ].y,
  460. mdl.vertexes [ mdl.faces[ii].v3 ].z);
  461. //glNormal3f(mdl.normals [ mdl.faces[ii].v1 ].x,
  462. // mdl.normals [ mdl.faces[ii].v1 ].y,
  463. // mdl.normals [ mdl.faces[ii].v1 ].z);
  464. }
  465. }
  466. glEnd();
  467. }
  468.  
  469.  
  470.  
  471. void
  472. reshape(int w, int h)
  473. {
  474. glViewport(0, 0, w, h); // Reset The Current Viewport
  475. glMatrixMode(GL_PROJECTION);
  476.  
  477. glLoadIdentity();
  478. gluPerspective(45, (GLfloat)w/(GLfloat)h, 1, 100);
  479.  
  480. glMatrixMode(GL_MODELVIEW);
  481. glLoadIdentity();
  482. }
  483.  
  484.  
  485.  
  486. void
  487. draw_filled_hex()
  488. {
  489. glBegin(GL_POLYGON);
  490. {
  491. glNormal3f(0, 0, 1);
  492.  
  493. glVertex2f(0, HEX_EX);
  494. glVertex2f(-HEX_IN, +HEX_IN/2);
  495. glVertex2f(-HEX_IN, -HEX_IN/2);
  496. glVertex2f(0, -HEX_EX);
  497. glVertex2f(HEX_IN, -HEX_IN/2);
  498. glVertex2f(HEX_IN, HEX_IN/2);
  499. }
  500. glEnd();
  501. }
  502.  
  503.  
  504.  
  505.  
  506. void
  507. draw_wire_hex()
  508. {
  509. #if(0)
  510. glLineStipple(1,0x00FF);
  511. glEnable(GL_LINE_STIPPLE);
  512. #endif
  513. glBegin(GL_LINE_STRIP);
  514. {
  515. glVertex2f(0, HEX_EX);
  516. glVertex2f(-HEX_IN, +HEX_IN/2);
  517. glVertex2f(-HEX_IN, -HEX_IN/2);
  518. glVertex2f(0, -HEX_EX);
  519. glVertex2f(HEX_IN, -HEX_IN/2);
  520. glVertex2f(HEX_IN, HEX_IN/2);
  521. }
  522. glEnd();
  523. glDisable(GL_LINE_STIPPLE);
  524. }
  525.  
  526.  
  527.  
  528. void
  529. draw_character(int x, int y, character *ch)
  530. {
  531. if ((x >= 0 && x < x_count) && (y >= 0 && y < y_count)) {
  532. glPushMatrix();
  533. {
  534. translate(y, x);
  535. {
  536. glTranslatef(0, 0, 0.01); //slightly above the floor
  537.  
  538. //player identifier
  539. if (ch->player == 1)
  540. glColor3f(1.0, 0.5, 0.5);
  541. if (ch->player == 2)
  542. glColor3f(0.5, 0.5, 1.0);
  543. glDisable(GL_TEXTURE_2D);
  544. gluDisk(gliquadric, HEX_IN*0.7, HEX_IN*0.9, 12, 2);
  545.  
  546. glColor3f(0.3, 1, 0.3);
  547. gluPartialDisk(gliquadric, HEX_IN*0.5, HEX_IN*0.7, ch->ap, 2, 0, ch->ap*60.0);
  548. }
  549. #if(0) //draw test
  550. glColor3f(1, 1, 1);
  551. glTranslatef(0, 0, 0.2); //slightly above the floor
  552. gluSphere(gliquadric, HEX_IN*0.3, 6, 6);
  553. #else //draw model
  554. glEnable(GL_TEXTURE_2D);
  555. // glEnable(GL_LIGHTING);
  556.  
  557. glColor3f(1, 1, 1);
  558. glRotatef(90, 1, 0, 0); // because of blender exporter
  559. glRotatef(ch->rot, 0, 1, 0); //individual character rotation
  560. //draw_model(mdl[ch->frame]);
  561. draw_model(mdl_static);
  562.  
  563. #if(1) //shadow
  564. glDisable(GL_TEXTURE_2D);
  565. glScalef(1, 0.01, 1);
  566. glColor4f(0, 0, 0, 0.5);
  567. draw_model(mdl_static);
  568. glEnable(GL_TEXTURE_2D);
  569. #endif
  570. #endif
  571. }
  572. glPopMatrix();
  573. }
  574. }
  575.  
  576.  
  577. #define PICK_BG 0
  578. #define PICK_HEX 1
  579.  
  580. void
  581. draw_for_picking()
  582. {
  583. glDisable(GL_TEXTURE_2D);
  584. // //glDisable(GL_LIGHTING);
  585.  
  586. int x, y;
  587.  
  588. glClearColor(0, 0, PICK_BG, 1);
  589. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  590.  
  591. glLoadIdentity();
  592.  
  593. //position
  594. {
  595. glTranslatef(0, 0, -6); // Translate 6 Units Into The Screen
  596. glRotatef(-ROTATE_ANGLE_2, 1, 0, 0);
  597. glRotatef(ROTATE_ANGLE, 0, 0, 1);
  598.  
  599. glTranslatef(camera_x, camera_y, 0);
  600. }
  601.  
  602. glPushMatrix();
  603. {
  604. for (y=0; y<y_count; y++) {
  605. for (x=0; x<x_count; x++) {
  606. glColor3ub(x, y, PICK_HEX);
  607. draw_filled_hex();
  608. glTranslatef(2*HEX_IN, 0, 0);
  609. }
  610. if (y%2 == 0)
  611. glTranslatef(-HEX_IN, 1.5*HEX_EX, 0);
  612. else
  613. glTranslatef(+HEX_IN, 1.5*HEX_EX, 0);
  614. glTranslatef(-(2*HEX_IN *x_count), 0, 0);
  615. }
  616. }
  617. glPopMatrix();
  618. // glEnable(GL_LIGHTING);
  619. }
  620.  
  621.  
  622. void
  623. draw()
  624. {
  625. glDisable(GL_TEXTURE_2D);
  626. //glDisable(GL_LIGHTING);
  627. //glShadeModel(GL_SMOOTH);
  628.  
  629. int x, y;
  630.  
  631. glClearColor(0, 0, 0, 1);
  632. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  633.  
  634. glLoadIdentity();
  635.  
  636. //position
  637. {
  638. glTranslatef(0, 0, -6); // Translate 6 Units Into The Screen
  639. glRotatef(-ROTATE_ANGLE_2, 1, 0, 0);
  640. //glRotatef(-60, 1, 0, 0);
  641. glRotatef(ROTATE_ANGLE, 0, 0, 1);
  642.  
  643. glTranslatef(camera_x, camera_y, 0);
  644. }
  645.  
  646. glPushMatrix();
  647.  
  648. for (y=0; y< y_count; y++) {
  649. for (x=0; x<x_count; x++) {
  650. glLineWidth(5);
  651.  
  652. if (IS_SOMEONE_SELECTED && sX == x && sY == y) {
  653. glPushMatrix();
  654. glColor3f(0.8, 0.3, 0.3);
  655. glTranslatef(0, 0, 0.01);
  656. gluDisk(gliquadric, HEX_IN*0.9, HEX_IN*0.95, 12, 2);
  657. glPopMatrix();
  658. }
  659.  
  660. if (PICKED_SMTH && aX == x && aY == y) {
  661. glPushMatrix();
  662. glColor3f(0.3, 0.8, 0.3);
  663. glTranslatef(0, 0, 0.01);
  664. gluDisk(gliquadric, HEX_IN*0.95, HEX_IN, 12, 2);
  665. glPopMatrix();
  666. }
  667.  
  668. if (IS_SOMEONE_SELECTED) {
  669. if (MAP[sX][sY].ch != NULL && MAP[x][y].move_cost <= MAP[sX][sY].ch->ap) {
  670. glPushMatrix();
  671. glColor3f(0.5, 0.5, 1.0);
  672. glTranslatef(0, 0, 0.01);
  673. gluDisk(gliquadric, HEX_IN*0.2, HEX_IN*0.4, 12, 2);
  674. glPopMatrix();
  675. }
  676. }
  677. float coff = 1.0 - (float)(MAP[x][y].init_cost*20)/256;
  678. glColor3f(coff, coff, coff);
  679. draw_filled_hex();
  680. #if(1) //draw wire
  681. #if(0) //color it as current player
  682. if (PLAYER == 1)
  683. glColor3f(1.0, 0.5, 0.5);
  684. else
  685. glColor3f(0.5, 0.5, 1.0);
  686. #else //gray wire
  687. glColor3f(0.5, 0.5, 0.5);
  688. #endif
  689. glLineWidth(3);
  690. draw_wire_hex();
  691. #endif //draw wire
  692. glTranslatef(2*HEX_IN, 0, 0);
  693. }
  694. if (y%2 == 0)
  695. glTranslatef(-HEX_IN, 1.5*HEX_EX, 0);
  696. else
  697. glTranslatef(+HEX_IN, 1.5*HEX_EX, 0);
  698. glTranslatef(-(2*HEX_IN *x_count), 0, 0);
  699. }
  700.  
  701. glPopMatrix();
  702.  
  703. glEnable(GL_TEXTURE_2D);
  704. //glEnable(GL_LIGHTING);
  705.  
  706. for (x=0; x<x_count; x++) {
  707. for (y=0; y<y_count; y++) {
  708. if (MAP[x][y].ch != NULL)
  709. draw_character(x, y, MAP[x][y].ch);
  710. }
  711. }
  712. }
  713.  
  714.  
  715.  
  716.  
  717. void
  718. do_picking()
  719. {
  720. GLint viewport[4];
  721. GLubyte pixel[3];
  722.  
  723. glGetIntegerv(GL_VIEWPORT, viewport);
  724. glReadPixels(cursorX, viewport[3]-cursorY, 1, 1, GL_RGB,
  725. GL_UNSIGNED_BYTE, (void*)pixel);
  726.  
  727.  
  728. aX = pixel[0];
  729. aY = pixel[1];
  730.  
  731. //--------------------------
  732.  
  733. PICKED_SMTH = (bool)pixel[2];
  734. printf("%u\n", pixel[2]);
  735. if (pixel[2] > 1) {
  736. //printf("some button\n");
  737. printf("%s\n", BUTTONS[ pixel[2]-2 ].name);
  738. }
  739. }
  740.  
  741.  
  742.  
  743. void
  744. fill_map(Q *q, ushort by, ushort bx)
  745. {
  746. ushort x, y;
  747. for (y=0; y<y_count; y++) {
  748. for (x=0; x<x_count; x++) {
  749. MAP[x][y].move_cost = 30000;
  750. }
  751. }
  752.  
  753. push(q, bx, by, bx, by, 0); //start point
  754.  
  755. while (pop(q, &x, &y)) {
  756. try_push(q, x+1, y , x, y);
  757. try_push(q, x-1, y , x, y);
  758. try_push(q, x , y+1, x, y);
  759. try_push(q, x , y-1, x, y);
  760. if (y%2==0) {
  761. try_push(q, x+1, y-1, x, y);
  762. try_push(q, x+1, y+1, x, y);
  763. } else {
  764. try_push(q, x-1, y-1, x, y);
  765. try_push(q, x-1, y+1, x, y);
  766. }
  767. }
  768. }
  769.  
  770.  
  771. ushort back_path_x[100];
  772. ushort back_path_y[100];
  773. ushort back_path_current = 0;
  774. //TODO
  775.  
  776. void
  777. get_path(ushort by, ushort bx, ushort ey, ushort ex)
  778. {
  779. back_path_current = 0;
  780.  
  781.  
  782. ushort x = ex, y = ey, tmpx, tmpy;
  783. while ((x != bx) || (y != by)) {
  784. back_path_x[back_path_current] = x;
  785. back_path_y[back_path_current] = y;
  786. back_path_current++;
  787.  
  788. tmpx = MAP[x][y].parent_x;
  789. tmpy = MAP[x][y].parent_y;
  790. x = tmpx;
  791. y = tmpy;
  792.  
  793. }
  794. back_path_x[back_path_current] = x;
  795. back_path_y[back_path_current] = y;
  796. back_path_current++;
  797. }
  798.  
  799.  
  800.  
  801. void
  802. end_player_turn()
  803. {
  804. IS_SOMEONE_SELECTED = false;
  805.  
  806. if (PLAYER == 1)
  807. PLAYER = 2;
  808. else
  809. PLAYER = 1;
  810.  
  811. int y, x;
  812. for (y=0; y<y_count; y++) {
  813. for (x=0; x<x_count; x++) {
  814. if (MAP[x][y].ch != NULL && MAP[x][y].ch->player == PLAYER) {
  815. MAP[x][y].ch->ap = 6;
  816. }
  817. }
  818. }
  819. }
  820.  
  821.  
  822.  
  823. void
  824. handle_mouse_events(SDL_Event E)
  825. {
  826.  
  827. if (PICKED_SMTH && MODE == MODE_SELECT) {
  828. if (MAP[aX][aY].ch != NULL) {
  829. if (MAP[aX][aY].ch->player == PLAYER) {
  830. // select character
  831. sX = aX;
  832. sY = aY;
  833. IS_SOMEONE_SELECTED = true;
  834. fill_map(&q, sY, sX);
  835. } else {
  836. if (IS_SOMEONE_SELECTED && MAP[sX][sY].ch->ap >= (3 + MAP[aX][aY].move_cost)) {
  837. // try to kill KILL enemy unit!
  838. if (rand()%2) {
  839. free(MAP[aX][aY].ch);
  840. MAP[aX][aY].ch = NULL;
  841. }
  842. MAP[sX][sY].ch->ap -= 3 + MAP[aX][aY].move_cost;
  843. }
  844. }
  845. } else {
  846. if (IS_SOMEONE_SELECTED
  847. && MAP[sX][sY].ch != NULL
  848. && MAP[sX][sY].ch->ap > 0) {
  849. // move selected character
  850. fill_map(&q, sY, sX);
  851. if (MAP[aX][aY].move_cost <= MAP[sX][sY].ch->ap) {
  852. //if character has needed action points
  853.  
  854. MAP[sX][sY].ch->ap -= MAP[aX][aY].move_cost;
  855. TMP_CHAR = MAP[sX][sY].ch;
  856. MAP[sX][sY].ch = NULL;
  857.  
  858. get_path(sY, sX, aY, aX);
  859. //START_MOVE(sX, sY, aX, aY);
  860. MODE = MODE_MOVE;
  861. }
  862. }
  863. }
  864. }
  865.  
  866. }
  867.  
  868.  
  869. bool IS_CAMERA_ROTATING = false;
  870.  
  871. void
  872. hanlde_events(SDL_Event E)
  873. {
  874. switch (E.type) {
  875. default:
  876. //puts("unknown event!");
  877. break;
  878.  
  879. case SDL_QUIT:
  880. isProgramLooping = false;
  881. break;
  882.  
  883. case SDL_VIDEORESIZE:
  884. reshape(E.resize.w, E.resize.h);
  885. break;
  886.  
  887. case SDL_MOUSEMOTION:
  888. cursorX = E.motion.x;
  889. cursorY = E.motion.y;
  890. if(IS_CAMERA_ROTATING ){
  891. ROTATE_ANGLE += E.motion.xrel;
  892. ROTATE_ANGLE_2 += E.motion.yrel;
  893. }
  894. break;
  895.  
  896. case SDL_MOUSEBUTTONDOWN:
  897. if(E.button.button == SDL_BUTTON_RIGHT){
  898. IS_CAMERA_ROTATING = true;
  899. }
  900. handle_mouse_events(E);
  901. break;
  902. case SDL_MOUSEBUTTONUP:
  903. if(E.button.button == SDL_BUTTON_RIGHT){
  904. IS_CAMERA_ROTATING = false;
  905. }
  906. handle_mouse_events(E);
  907. break;
  908.  
  909.  
  910. case SDL_KEYDOWN:
  911. switch (E.key.keysym.sym) {
  912. default:
  913. break;
  914. case SDLK_q:
  915. case SDLK_ESCAPE:
  916. isProgramLooping = false;
  917. break;
  918. case SDLK_SPACE:
  919. end_player_turn();
  920. break;
  921. case SDLK_UP:
  922. camera_y--;
  923. break;
  924. case SDLK_DOWN:
  925. camera_y++;
  926. break;
  927. case SDLK_LEFT:
  928. camera_x++;
  929. break;
  930. case SDLK_RIGHT:
  931. camera_x--;
  932. break;
  933. }
  934. }
  935. }
  936.  
  937.  
  938.  
  939. void
  940. init_sdl()
  941. {
  942. SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
  943. atexit(SDL_Quit);
  944.  
  945. /* the flags to pass to SDL_SetVideoMode */
  946. video_flags = SDL_OPENGL;
  947. video_flags |= SDL_GL_DOUBLEBUFFER;
  948. video_flags |= SDL_RESIZABLE;
  949.  
  950. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  951. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
  952. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  953. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  954. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  955. /* get a SDL surface */
  956. surface = SDL_SetVideoMode(SCR_WIDTH, SCR_HEIGHT,
  957. SCR_BPP, video_flags);
  958. SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, 0, 0, 0, 0));
  959.  
  960. reshape(SCR_WIDTH, SCR_HEIGHT);
  961. }
  962.  
  963.  
  964.  
  965. void
  966. init_opengl()
  967. {
  968. glEnable(GL_TEXTURE_2D);
  969. glShadeModel(GL_SMOOTH);
  970.  
  971. glClearColor(0, 0, 0, 1);
  972. glClearDepth(1.0f);
  973. glEnable(GL_DEPTH_TEST);
  974.  
  975. //glEnable(GL_LIGHTING);
  976. glEnable(GL_AUTO_NORMAL);
  977. glEnable(GL_COLOR_MATERIAL);
  978. glEnable(GL_LIGHT0);
  979. glEnable(GL_NORMALIZE);
  980. float POS[] = {0, 1, 0, 0};
  981. glLightfv(GL_LIGHT0, GL_POSITION, POS);
  982.  
  983. glDepthFunc(GL_LEQUAL); /* The Type Of Depth Test To Do */
  984. /* Really Nice Perspective Calculations */
  985. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  986. glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  987. glLineWidth(4);
  988.  
  989.  
  990. //glEnable(GL_CULL_FACE);
  991. //glFrontFace(GL_CW);
  992. //glCullFace(GL_BACK);
  993.  
  994. //glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
  995. }
  996.  
  997.  
  998.  
  999.  
  1000. bool
  1001. create_char(int x, int y, int player)
  1002. {
  1003. MAP[y][x].ch = malloc(sizeof(character));
  1004. character *ch = malloc(sizeof(character));
  1005.  
  1006. ch->player = player;
  1007. ch->rot = (rand()%6)*60+30;
  1008. ch->frame = 0;
  1009. ch->ap = 6;
  1010.  
  1011. MAP[y][x].ch = ch;
  1012.  
  1013. return(true);
  1014. }
  1015.  
  1016.  
  1017.  
  1018. void
  1019. init_map()
  1020. {
  1021. int i, j;
  1022. for (i=0; i<y_count; i++) {
  1023. for (j=0; j<x_count; j++) {
  1024. MAP[i][j].ch = NULL;
  1025. MAP[i][j].init_cost = (rand()%2)*3+1;
  1026. MAP[i][j].move_cost = 30000;
  1027.  
  1028. if (rand()%10 > 7)
  1029. create_char(j, i, rand()%2+1);
  1030. }
  1031. }
  1032. }
  1033.  
  1034.  
  1035.  
  1036. void
  1037. move_logic()
  1038. {
  1039. if (SPECIAL_STEPS > 0) { // we have not reached the destination
  1040. SPECIAL_X += SPECIAL_STEP_X;
  1041. SPECIAL_Y += SPECIAL_STEP_Y;
  1042. SPECIAL_STEPS--;
  1043. } else {
  1044. if (back_path_current > 1) {
  1045. back_path_current--;
  1046. START_MOVE(back_path_x[back_path_current],
  1047. back_path_y[back_path_current],
  1048. back_path_x[back_path_current-1],
  1049. back_path_y[back_path_current-1]
  1050. );
  1051. } else { // END
  1052. MODE = MODE_SELECT;
  1053. MAP[ex][ey].ch = TMP_CHAR;
  1054. sX = ex;
  1055. sY = ey;
  1056. fill_map(&q, sY, sX);
  1057. }
  1058. }
  1059. }
  1060.  
  1061.  
  1062.  
  1063. void
  1064. move_draw()
  1065. {
  1066. glTranslatef(SPECIAL_X, SPECIAL_Y, 0);
  1067. //glTranslatef(0, 0, 0.2); //slightly above the floor
  1068. glRotatef(90, 1, 0, 0); // because of blender exporter
  1069.  
  1070. if (SPECIAL_STEP_Y <= 0)
  1071. glRotatef(SPECIAL_ANGLE, 0, 1, 0);
  1072. else
  1073. glRotatef(SPECIAL_ANGLE, 0, -1, 0);
  1074.  
  1075. glColor3f(1, 1, 1);
  1076. #if(0) //test
  1077. glTranslatef(0, 0, 0.2); //slightly above the floor
  1078. gluSphere(gliquadric, HEX_IN*0.3, 6, 6);
  1079. #else //draw character
  1080. draw_model(mdl[FRAME]);
  1081. #if(1) //shadow
  1082. glScalef(1, 0.01, 1);
  1083. glColor4f(0, 0, 0, 0.5);
  1084. draw_model(mdl[FRAME]);
  1085. #endif //~shadow
  1086. #endif //~test
  1087. }
  1088.  
  1089.  
  1090.  
  1091. unsigned
  1092. generate_texture_from_str(char* Text, int *w, int *h)
  1093. {
  1094. //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1095.  
  1096. SDL_Color Color = {255, 255, 255};
  1097. SDL_Surface *txt = TTF_RenderUTF8_Blended(font, Text, Color);
  1098. unsigned Texture = 0;
  1099.  
  1100. glGenTextures(1, &Texture); /*Generate an OpenGL 2D texture from the SDL_Surface*.*/
  1101.  
  1102. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, txt->w, txt->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, txt->pixels);
  1103. *w = txt->w;
  1104. *h = txt->h;
  1105. SDL_FreeSurface(txt);
  1106.  
  1107. return(Texture);
  1108. }
  1109.  
  1110.  
  1111.  
  1112. bool
  1113. add_button(int x, int y, int W, int H, char* name, char *text)
  1114. {
  1115. unsigned textureID;
  1116. int w, h;
  1117. textureID = generate_texture_from_str(text, &w, &h);
  1118.  
  1119. if (textureID == 0)
  1120. exit(1);
  1121.  
  1122. gui_button b = {x, y, W, H, name, textureID, w, h};
  1123.  
  1124. BUTTONS[current_button] = b;
  1125. current_button++;
  1126.  
  1127. return(true);
  1128. }
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134. void
  1135. draw_button(gui_button b)
  1136. {
  1137. glPushMatrix();
  1138. {
  1139. glTranslatef(b.x, b.y, 0);
  1140.  
  1141. glColor4f(0.3, 0.3, 0.3, 0.8);
  1142. glBegin(GL_QUADS);
  1143. {
  1144. glVertex3d(0, 0, 0);
  1145. glVertex3d(b.sx, 0, 0);
  1146. glVertex3d(b.sx, b.sy, 0);
  1147. glVertex3d(0, b.sy, 0);
  1148. }
  1149. glEnd();
  1150.  
  1151. glColor4f(1, 1, 1, 0.8);
  1152. glScalef(0.3, 0.3, 0.3);
  1153.  
  1154. //RENDER TEXT
  1155. {
  1156. glEnable(GL_TEXTURE_2D);
  1157.  
  1158. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1159. glBindTexture(GL_TEXTURE_2D, b.textureID);
  1160.  
  1161. //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1162. //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1163.  
  1164. glBegin(GL_QUADS);
  1165. {
  1166. glTexCoord2d(0, 0);
  1167. glVertex2d(0, 0);
  1168. glTexCoord2d(1, 0);
  1169. glVertex2d(b.textSX, 0);
  1170. glTexCoord2d(1, 1);
  1171. glVertex2d(b.textSX, b.textSY);
  1172. glTexCoord2d(0, 1);
  1173. glVertex2d(0, b.textSY);
  1174. }
  1175. glEnd();
  1176.  
  1177. glDisable(GL_TEXTURE_2D);
  1178. }
  1179. }
  1180. glPopMatrix();
  1181. }
  1182.  
  1183.  
  1184.  
  1185. void
  1186. draw_button_for_picking(gui_button b)
  1187. {
  1188. glPushMatrix();
  1189. {
  1190. glTranslatef(b.x, b.y, 0);
  1191. glBegin(GL_QUADS);
  1192. {
  1193. glVertex3d(0, 0, 0);
  1194. glVertex3d(b.sx, 0, 0);
  1195. glVertex3d(b.sx, b.sy, 0);
  1196. glVertex3d(0, b.sy, 0);
  1197. }
  1198. glEnd();
  1199. }
  1200. glPopMatrix();
  1201. }
  1202.  
  1203.  
  1204.  
  1205.  
  1206. void
  1207. draw_interface()
  1208. {
  1209. glEnable(GL_TEXTURE_2D);
  1210. glEnable(GL_BLEND);
  1211.  
  1212. glDisable(GL_DEPTH_TEST);
  1213.  
  1214. glMatrixMode(GL_PROJECTION);
  1215. glPushMatrix();
  1216. glLoadIdentity();
  1217. glOrtho(0, SCR_WIDTH, SCR_HEIGHT, 0, 0, 1);
  1218.  
  1219. glMatrixMode(GL_MODELVIEW);
  1220. glPushMatrix();
  1221. {
  1222. glLoadIdentity();
  1223.  
  1224. int i;
  1225. for (i = 0; i < current_button; i++)
  1226. draw_button(BUTTONS[i]);
  1227. }
  1228. glPopMatrix();
  1229.  
  1230. glMatrixMode(GL_PROJECTION);
  1231. glPopMatrix();
  1232.  
  1233. glMatrixMode(GL_MODELVIEW);
  1234.  
  1235. glEnable(GL_DEPTH_TEST);
  1236. glDisable(GL_TEXTURE_2D);
  1237. glDisable(GL_BLEND);
  1238. }
  1239.  
  1240.  
  1241. void
  1242. draw_interface_for_picking()
  1243. {
  1244. glDisable(GL_DEPTH_TEST);
  1245.  
  1246. glMatrixMode(GL_PROJECTION);
  1247. glPushMatrix();
  1248. glLoadIdentity();
  1249. glOrtho(0, SCR_WIDTH, SCR_HEIGHT, 0, 0, 1);
  1250.  
  1251. glMatrixMode(GL_MODELVIEW);
  1252. glPushMatrix();
  1253. {
  1254. glLoadIdentity();
  1255.  
  1256. int i;
  1257. for (i = 0; i < current_button; i++) {
  1258. glColor3ub(0, 0, i+2);
  1259. draw_button_for_picking(BUTTONS[i]);
  1260. }
  1261. }
  1262. glPopMatrix();
  1263.  
  1264. glMatrixMode(GL_PROJECTION);
  1265. glPopMatrix();
  1266.  
  1267. glMatrixMode(GL_MODELVIEW);
  1268.  
  1269. glEnable(GL_DEPTH_TEST);
  1270. }
  1271.  
  1272.  
  1273.  
  1274. void
  1275. init_interface()
  1276. {
  1277. if (TTF_Init() == -1) {
  1278. printf("Unable to initialize SDL_ttf: %s \n", TTF_GetError());
  1279. exit(1);
  1280. }
  1281. font = TTF_OpenFont("font.ttf", 60);
  1282.  
  1283.  
  1284. add_button(0, 0, 80, 25, "player identifier", "красный");
  1285. add_button(300, 300, 80, 25, "test button 1", "butt 1");
  1286. add_button(350, 350, 80, 25, "test button 2", "butt 2");
  1287. }
  1288.  
  1289.  
  1290. #undef main //write straight to the cosole
  1291.  
  1292. int
  1293. main(void)
  1294. {
  1295. gliquadric = gluNewQuadric();
  1296. SDL_Event E;
  1297. Uint32 next_time;
  1298.  
  1299. srand(time(0));
  1300.  
  1301. if (init_q(&q, 66000) == false) {
  1302. puts("can't init Qu!");
  1303. return(1);
  1304. }
  1305.  
  1306. //load_texture("reegie1.3.1__RIFF_1.bmp");
  1307. mdl_static = read_obj_file("test2/reegie1.3.1__RIFF_3_000010.obj");
  1308. int i;
  1309. for (i = 0; i < 32; i++) {
  1310. char obj_file_name[100];
  1311. sprintf(obj_file_name, "test2/reegie1.3.1__RIFF_3_0000%02i.obj", i+1);
  1312. mdl[i] = read_obj_file(obj_file_name);
  1313. }
  1314.  
  1315. init_sdl();
  1316. init_opengl();
  1317. init_interface();
  1318. init_map();
  1319.  
  1320.  
  1321.  
  1322. HEX_EX = 0.4f;
  1323. HEX_IN = sqrt(HEX_EX*HEX_EX - (HEX_EX/2)*(HEX_EX/2));
  1324.  
  1325. //printf("%i\n", __LINE__);
  1326.  
  1327. next_time = SDL_GetTicks() + 300;
  1328. while (isProgramLooping) {
  1329. //HANDLE EVENTS
  1330. {
  1331. while (SDL_PollEvent(&E))
  1332. hanlde_events(E);
  1333. }
  1334.  
  1335. //PICK
  1336. {
  1337. draw_for_picking();
  1338. draw_interface_for_picking(font);
  1339. do_picking();
  1340. }
  1341.  
  1342. //DRAW
  1343. {
  1344. draw();
  1345.  
  1346. if (MODE == MODE_MOVE)
  1347. move_draw();
  1348. //if(MODE == MODE_ATACK)
  1349. // atack();
  1350. //if(MODE == MODE_ROTATE)
  1351. // rotate();
  1352.  
  1353. draw_interface(font);
  1354. SDL_GL_SwapBuffers();
  1355. }
  1356.  
  1357. //LOGIC
  1358. {
  1359. if (MODE == MODE_MOVE)
  1360. move_logic();
  1361. }
  1362.  
  1363. //TIME/FPS
  1364. {
  1365. if (next_time > SDL_GetTicks())
  1366. //SDL_Delay(next_time - SDL_GetTicks());
  1367. ;
  1368. else
  1369. next_time += 20; //SHIT
  1370.  
  1371. FRAME++;
  1372. if (FRAME == 32)
  1373. FRAME = 0;
  1374.  
  1375. next_time += 20;
  1376. }
  1377. }
  1378.  
  1379. return 0;
  1380. }

Report this snippet  

You need to login to post a comment.