1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include <stdio.h>
#include <windows.h>
#include <GLUT/glut.h>
#include "gl_warsim.h"
float old_x;
float old_y;
//INITIALISATION
#define TROOP_MAX 3
void initialisation (void)
{
int i;
float x, y;
troop tmp;
if ((tab_troop = (troop *) malloc (TROOP_MAX * sizeof (troop))) < 0)
{
printf ("--> initialisation error : tab_troop malloc\n");
exit (-1);
}
for (i = 0, x = -7, y = -2; i < TROOP_MAX; i++)
{
tmp = tab_troop[i];
tmp.id = i + 1;
tmp.coord.x = x;
tmp.coord.y = y;
tmp.selected = 0;
tmp.life = LIFE_INF;
tmp.type = 1;
tmp.name = 0;
tab_troop[i] = tmp;
x += TROOP_SIZE * LIFE_INF + 0.5;
printf ("id = %d\nx = %f ; y = %f\n--\n", tmp.id, tmp.coord.x, tmp.coord.y);
}
printf ("*** initiation successfull ***\n");
}
//DRAW_REGIMENT : draw a regiment
void draw_regiment (int id)
{
troop tmp;
float x, y;
int nb;
int selected;
printf ("drawing regiment %d\n", id + 1);
tmp = tab_troop[id];
x = tmp.coord.x;
y = tmp.coord.y;
nb = tmp.life;
selected = tmp.selected;
printf ("x = %f ; y = %f ; nb = %d\n", x, y, nb);
glLoadName (id + 1);
glBegin (GL_QUADS);
if (selected) glColor3f (TROOP_COLOR1_SR, TROOP_COLOR1_SG, TROOP_COLOR1_SB);
else glColor3f (TROOP_COLOR1_R, TROOP_COLOR1_G, TROOP_COLOR1_B);
glVertex2f (x, y);
glVertex2f (x + (TROOP_SIZE * nb), y);
glVertex2f (x + (TROOP_SIZE * nb), y - 1);
glVertex2f (x, y - 1);
glEnd ();
}
//DRAW_TROOPS : draw troops with tab_troop
void draw_troops (void)
{
int i;
for (i = 0; i < TROOP_MAX; i++) draw_regiment (i);
}
//RESHAPE
void reshape (int w, int h)
{
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity ();
glMatrixMode (GL_MODELVIEW); // Select The Model View Matrix
glLoadIdentity (); // Reset The Model View Matrix
}
void draw_battlefield (void)
{
glBegin(GL_QUADS);
glColor3f (0.4, 0.8, 0.5);
glVertex3f(-9, 9, 0);
glVertex3f(9, 9, 0);
glVertex3f(9, -9, 0);
glVertex3f(-9, -9, 0);
glEnd ();
}
void draw_scene ()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
//glTranslated (0, 0, 2); <=====
gluOrtho2D(-10,10,-10,10);
draw_battlefield ();
draw_troops ();
}
void display (void)
{
draw_scene ();
glFlush ();
glutSwapBuffers ();
}
//main function
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(600,600);
glutInitWindowPosition(20, 20);
glutCreateWindow("WARGAME SIMULATION v0.02");
glutDisplayFunc (display);
glutReshapeFunc (reshape);
initialisation ();
glutMainLoop();
return 0;
} |