/
draw.c
127 lines (96 loc) · 2.6 KB
/
draw.c
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
/*
draw.c
This file is a part of Test Stuff 2.x source code.
Comes under the terms of GNU General Public License v.2.0.
*/
#include <GL/gl.h>
#include "boss.h"
#include "draw.h"
#include "text.h"
#include "menu.h"
#include "idiot.h"
#include "items.h"
#include "level.h"
#include "bullet.h"
#include "player.h"
#include "turret.h"
#include "portal.h"
#include "gameover.h"
#include "drunkenbot.h"
void drawImage(t_Image *image, int x, int y, SDL_Rect *draw_rect)
{
#ifdef DEBUG
if(!image->gl_tex)
return;
#endif
static SDL_Rect r;
if (draw_rect) {
r.x = draw_rect->x;
r.y = draw_rect->y;
r.w = draw_rect->w;
r.h = draw_rect->h;
}
else {
r.x = 0;
r.y = 0;
r.w = image->w;
r.h = image->h;
}
glColor4f(1,1,1,1);
GLuint sprite = image->gl_tex;
glBindTexture(GL_TEXTURE_2D, sprite);
float W = image->w;
float H = image->h;
float dX = r.w;
float dY = r.h;
glEnable(GL_TEXTURE_2D);
glLoadIdentity();
glTranslatef(x,y,0);
glBegin(GL_QUADS);
glTexCoord2f((r.x/W),(r.y/H)); glVertex2f(0, 0); // topleft
glTexCoord2f((r.x/W),((r.y+r.h)/H)); glVertex2f(0, dY); // bottomleft
glTexCoord2f(((r.x+r.w)/W),((r.y+r.h)/H)); glVertex2f(dX,dY); // bottomright
glTexCoord2f(((r.x+r.w)/W),(r.y/H)); glVertex2f(dX, 0); // topright
glEnd();
glLoadIdentity();
glColor4f(1,1,1,1);
}
void draw()
{
glColor4f(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
switch(appState)
{
case APPSTATE_INTRO:
drawImage(&images[IMG_INTRO], SCRW/2-images[IMG_INTRO].w/2,
SCRH/2-images[IMG_INTRO].h/2, NULL);
break;
case APPSTATE_MENU:
menuDraw();
break;
case APPSTATE_GAME:
levelDrawAllTheRest();
itemsDraw();
portalDraw();
bulletDraw(1); /* bullets shot by player */
playerDraw();
idiotDraw();
drunkenbotDraw();
bulletDraw(0); /* all the rest */
turretDraw();
bossDraw();
levelDrawForeground();
/* Darkness layer here */
if(currentLevel.flags & IS_DARK_LEVEL)
drawImage(&images[IMG_DARKNESS], player.posX-(SCRW+STEP/2), player.posY-(SCRH+STEP/2), NULL);
/* Boss' HP bar */
bossDrawHPBar();
break;
case APPSTATE_GAMEOVER:
gameoverDraw();
break;
}
/* Text line */
textDraw();
SDL_GL_SwapBuffers();
}