Debian Codegraph @

engine/video/glgraphicdraw.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name glgraphicdraw.cpp - The general sprite functions. */
11 //
12 // (c) Copyright 2000-2008 by Lutz Sammer, Stephan Rasenberg,
13 // Nehal Mistry, and Jimmy Salmon
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29
30 //@{
31
32 /*----------------------------------------------------------------------------
33 -- Includes
34 ----------------------------------------------------------------------------*/
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "stratagus.h"
41 #include "video.h"
42
43 /*----------------------------------------------------------------------------
44 -- Functions
45 ----------------------------------------------------------------------------*/
46
47 /** Draw a rectangular part of a CGraphic to the screen.
48 **
49 ** This function does not attempt to clip the CGraphic based on the
50 ** screen coordinates. If the caller wants clipping, it can set the
51 ** parameters accordingly, or perhaps configure OpenGL to clip the
52 ** output.
53 **
54 ** @param g
55 ** The graphic to be drawn. It may consist of multiple
56 ** OpenGL textures if it is too large to fit in one texture.
57 ** @param textures
58 ** The OpenGL textures to be drawn. There must be g->NumTextures
59 ** elements in the array. These textures may be the same as
60 ** g->Textures, or perhaps variants of them with different colors
61 ** for a specific player.
62 ** @param gx_beg
63 ** X coordinate of the left side of the rectangle to be drawn from @a *g.
64 ** @param gy_beg
65 ** Y coordinate of the top of the rectangle to be drawn from @a *g.
66 ** @param gx_end
67 ** X coordinate of the right side of the rectangle to be drawn from @a *g.
68 ** @param gy_end
69 ** Y coordinate of the bottom of the rectangle to be drawn from @a *g.
70 ** @param sx_beg
71 ** X coordinate of the left side of the graphic on the screen.
72 ** @param sy_beg
73 ** Y coordinate of the top of the graphic on the screen.
74 ** @param flip
75 ** Whether to flip the graphic in the X direction.
76 ** In any case, the graphic will extend from @a sx_beg
77 ** to (@a gx_end - @a gx_beg + @a sx_beg) on the screen.
78 ** Flipping controls which of those values corresponds
79 ** to @a gx_beg and which one to @a gx_end.
80 */
81 void DrawTexture(const CGraphic *g, GLuint *textures,
82 int gx_beg, int gy_beg, int gx_end, int gy_end,
83 int sx_beg, int sy_beg, int flip)
84 {
85 // gx and gy coordinates count pixels from the top left corner
86 // of the CGraphic, which can span multiple textures.
87 // tx and ty coordinates are in an individual texture,
88 // as GLfloats between 0.0 and 1.0, like OpenGL requires.
89 // sx and sy coordinates are on the screen. This function does not
90 // know what the origin is there.
91
92 Assert(0 <= gx_beg);
93 Assert(0 <= gy_beg);
94 Assert(gx_beg <= gx_end); // draws nothing if equal
95 Assert(gy_beg <= gy_end); // draws nothing if equal
96 Assert(gx_end <= g->GraphicWidth);
97 Assert(gy_end <= g->GraphicHeight);
98
99 for (int tex_gy_beg = gy_beg / GLMaxTextureSize * GLMaxTextureSize;;
100 tex_gy_beg += GLMaxTextureSize) {
101 int tex_gy_end = tex_gy_beg + GLMaxTextureSize;
102 int clip_gy_beg = std::max(gy_beg, tex_gy_beg);
103 int clip_gy_end = std::min(gy_end, tex_gy_end);
104 if (clip_gy_beg >= clip_gy_end)
105 break;
106
107 int clip_sy_beg = clip_gy_beg - gy_beg + sy_beg;
108 int clip_sy_end = clip_gy_end - gy_beg + sy_beg;
109 Assert(clip_sy_end != clip_sy_beg);
110 Assert(abs(clip_sy_end - clip_sy_beg) <= g->GraphicHeight);
111
112 GLfloat clip_ty_beg, clip_ty_end;
113 if (tex_gy_end >= g->GraphicHeight) {
114 // This is the last row of textures in
115 // the Y direction. These textures may
116 // be smaller than the ones at the top.
117 clip_ty_beg = (clip_gy_beg - tex_gy_beg)
118 * g->TextureHeight
119 / (g->GraphicHeight - tex_gy_beg);
120 clip_ty_end = (clip_gy_end - tex_gy_beg)
121 * g->TextureHeight
122 / (g->GraphicHeight - tex_gy_beg);
123 } else {
124 clip_ty_beg = (clip_gy_beg - tex_gy_beg)
125 / GLfloat(GLMaxTextureSize);
126 clip_ty_end = (clip_gy_end - tex_gy_beg)
127 / GLfloat(GLMaxTextureSize);
128 }
129 Assert(0.0f <= clip_ty_beg);
130 Assert(clip_ty_beg < clip_ty_end);
131 Assert(clip_ty_end <= 1.0f);
132
133 for (int tex_gx_beg = gx_beg / GLMaxTextureSize
134 * GLMaxTextureSize;;
135 tex_gx_beg += GLMaxTextureSize) {
136 int tex_gx_end = tex_gx_beg + GLMaxTextureSize;
137 int clip_gx_beg = std::max(gx_beg, tex_gx_beg);
138 int clip_gx_end = std::min(gx_end, tex_gx_end);
139 if (clip_gx_beg >= clip_gx_end)
140 break;
141
142 // Flipping does not change which parts of the
143 // CGraphic get drawn. It only changes where
144 // they get drawn.
145 int clip_sx_beg, clip_sx_end;
146 if (flip) {
147 clip_sx_beg = sx_beg + (gx_end - clip_gx_beg);
148 clip_sx_end = sx_beg + (gx_end - clip_gx_end);
149 } else {
150 clip_sx_beg = sx_beg + (clip_gx_beg - gx_beg);
151 clip_sx_end = sx_beg + (clip_gx_end - gx_beg);
152 }
153 Assert(clip_sx_end != clip_sx_beg);
154 Assert(abs(clip_sx_end - clip_sx_beg) <= g->GraphicWidth);
155
156 GLfloat clip_tx_beg, clip_tx_end;
157 if (tex_gx_end >= g->GraphicWidth) {
158 // This is the last column of textures in
159 // the X direction. These textures may
160 // be smaller than the ones at the left.
161 clip_tx_beg = (clip_gx_beg - tex_gx_beg)
162 * g->TextureWidth
163 / (g->GraphicWidth - tex_gx_beg);
164 clip_tx_end = (clip_gx_end - tex_gx_beg)
165 * g->TextureWidth
166 / (g->GraphicWidth - tex_gx_beg);
167 } else {
168 clip_tx_beg = (clip_gx_beg - tex_gx_beg)
169 / GLfloat(GLMaxTextureSize);
170 clip_tx_end = (clip_gx_end - tex_gx_beg)
171 / GLfloat(GLMaxTextureSize);
172 }
173 Assert(0.0f <= clip_tx_beg);
174 Assert(clip_tx_beg < clip_tx_end);
175 Assert(clip_tx_end <= 1.0f);
176
177 int texture = tex_gy_beg / GLMaxTextureSize
178 * ((g->GraphicWidth - 1) / GLMaxTextureSize + 1)
179 + tex_gx_beg / GLMaxTextureSize;
180 Assert(texture >= 0 && texture < g->NumTextures);
181
182 glBindTexture(GL_TEXTURE_2D, textures[texture]);
183 glBegin(GL_QUADS);
184 glTexCoord2f(clip_tx_beg, clip_ty_beg);
185 glVertex2i(clip_sx_beg, clip_sy_beg);
186 glTexCoord2f(clip_tx_beg, clip_ty_end);
187 glVertex2i(clip_sx_beg, clip_sy_end);
188 glTexCoord2f(clip_tx_end, clip_ty_end);
189 glVertex2i(clip_sx_end, clip_sy_end);
190 glTexCoord2f(clip_tx_end, clip_ty_beg);
191 glVertex2i(clip_sx_end, clip_sy_beg);
192 glEnd();
193 }
194 }
195 }
196
197 //@}