1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | #define SG_BUILD_LIBRARY |
16 | #include <siege/graphics/surface.h> |
17 | #include <siege/graphics/bitmap.h> |
18 | #include <siege/graphics/draw.h> |
19 | #include <siege/core/window.h> |
20 | |
21 | #include <stdlib.h> |
22 | #include <stdio.h> |
23 | #include <math.h> |
24 | |
25 | #include "../internal/gl.h" |
26 | |
27 | SGbool SG_CALL__cdecl _sgSurfaceInit(void) |
28 | { |
29 | return SG_TRUE1; |
30 | } |
31 | SGbool SG_CALL__cdecl _sgSurfaceDeinit(void) |
32 | { |
33 | return SG_TRUE1; |
34 | } |
35 | |
36 | SGSurface* SG_CALL__cdecl sgSurfaceCreateBitmap(SGBitmap* bmp, SGbool delbmp) |
37 | { |
38 | size_t width; |
39 | size_t height; |
40 | SGenum bpp; |
41 | void* data; |
42 | |
43 | sgBitmapGetData(bmp, &width, &height, &bpp, &data); |
44 | SGSurface* surface = sgSurfaceCreateData(width, height, bpp, data); |
45 | if(delbmp) |
46 | sgBitmapDestroy(bmp); |
47 | return surface; |
48 | } |
49 | SGSurface* SG_CALL__cdecl sgSurfaceCreateStream(SGStream* stream, SGbool delstream) |
50 | { |
51 | SGBitmap* bmp = sgBitmapCreateStream(stream, delstream); |
52 | if(!bmp) return NULL((void*)0); |
53 | return sgSurfaceCreateBitmap(bmp, SG_TRUE1); |
54 | } |
55 | SGSurface* SG_CALL__cdecl sgSurfaceCreateFile(const char* fname) |
56 | { |
57 | SGStream* stream = sgStreamCreateFile(fname, "r"); |
58 | if(!stream) |
59 | { |
60 | fprintf(stderr(&_iob[2]), "Could not load image %s\n", fname); |
61 | return NULL((void*)0); |
62 | } |
63 | return sgSurfaceCreateStream(stream, SG_TRUE1); |
64 | } |
65 | SGSurface* SG_CALL__cdecl sgSurfaceCreateData(SGuint width, SGuint height, SGenum bpp, void* data) |
66 | { |
67 | SGTexture* texture = sgTextureCreateData(width, height, bpp, data); |
68 | if(!texture) return NULL((void*)0); |
| 2 | | Assuming 'texture' is non-null | |
|
| |
69 | |
70 | SGSurface* surface = sgSurfaceCreateTexture(texture, SG_TRUE1); |
| 4 | | Calling 'sgSurfaceCreateTexture' | |
|
71 | if(!surface) |
72 | sgTextureDestroy(texture); |
73 | return surface; |
74 | } |
75 | SGSurface* SG_CALL__cdecl sgSurfaceCreateTexture(SGTexture* texture, SGbool deltex) |
76 | { |
77 | SGSurface* surface = malloc(sizeof(SGSurface)); |
| |
78 | if(!surface) return NULL((void*)0); |
| 6 | | Assuming 'surface' is non-null | |
|
| |
79 | |
80 | surface->fboid = malloc(sizeof(GLuint)); |
81 | surface->rbid = malloc(sizeof(GLuint)); |
82 | surface->texture = NULL((void*)0); |
83 | surface->deltex = SG_FALSE0; |
84 | |
85 | glGenFramebuffersEXT(1, surface->fboid); |
86 | glGenRenderbuffersEXT(1, surface->rbid); |
87 | |
88 | if(!sgSurfaceSetTexture(surface, texture, deltex)) |
| |
89 | { |
90 | surface->texture = NULL((void*)0); |
91 | sgSurfaceDestroy(surface); |
| 9 | | Calling 'sgSurfaceDestroy' | |
|
| 12 | | Returning; memory was released via 1st parameter | |
|
92 | } |
93 | |
94 | return surface; |
| 13 | | Use of memory after it is freed |
|
95 | } |
96 | SGSurface* SG_CALL__cdecl sgSurfaceCreate(SGuint width, SGuint height, SGenum bpp) |
97 | { |
98 | return sgSurfaceCreateData(width, height, bpp, NULL((void*)0)); |
| 1 | Calling 'sgSurfaceCreateData' | |
|
99 | } |
100 | void SG_CALL__cdecl sgSurfaceDestroy(SGSurface* surface) |
101 | { |
102 | glDeleteRenderbuffersEXT(1, surface->rbid); |
103 | glDeleteFramebuffersEXT(1, surface->fboid); |
104 | free(surface->rbid); |
105 | free(surface->fboid); |
106 | if(surface->deltex && surface->texture) |
| |
107 | sgTextureDestroy(surface->texture); |
108 | free(surface); |
| |
109 | } |
110 | |
111 | SGbool SG_CALL__cdecl sgSurfaceSetTexture(SGSurface* surface, SGTexture* texture, SGbool deltex) |
112 | { |
113 | if(!texture) return SG_FALSE0; |
114 | if(surface->deltex) |
115 | { |
116 | if(surface->texture != texture) |
117 | sgTextureDestroy(surface->texture); |
118 | } |
119 | surface->texture = texture; |
120 | surface->deltex = deltex; |
121 | |
122 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT0x8D40, GLFBO(surface)(*(GLuint*)(surface)->fboid)); |
123 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT0x8D40, GL_COLOR_ATTACHMENT0_EXT0x8CE0, GL_TEXTURE_2D0x0DE1, GLTEX(texture)(*(GLuint*)(texture)->handle), 0); |
124 | |
125 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT0x8D41, GLRB(surface)(*(GLuint*)(surface)->rbid)); |
126 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT0x8D41, GL_DEPTH_COMPONENT240x81A6, texture->width, texture->height); |
127 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT0x8D40, GL_DEPTH_ATTACHMENT_EXT0x8D00, GL_RENDERBUFFER_EXT0x8D41, GLRB(surface)(*(GLuint*)(surface)->rbid)); |
128 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT0x8D41, 0); |
129 | |
130 | GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT0x8D40); |
131 | |
132 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT0x8D40, 0); |
133 | return status == GL_FRAMEBUFFER_COMPLETE_EXT0x8CD5; |
134 | } |
135 | SGTexture* SG_CALL__cdecl sgSurfaceGetTexture(SGSurface* surface) |
136 | { |
137 | return surface->texture; |
138 | } |
139 | |
140 | void SG_CALL__cdecl sgSurfaceSetData(SGSurface* surface, size_t width, size_t height, SGenum bpp, void* data) |
141 | { |
142 | sgTextureSetData(surface->texture, width, height, bpp, data); |
143 | sgSurfaceSetTexture(surface, surface->texture, surface->deltex); |
144 | } |
145 | void SG_CALL__cdecl sgSurfaceSetSubData(SGSurface* surface, size_t x, size_t y, size_t width, size_t height, SGenum bpp, void* data) |
146 | { |
147 | sgTextureSetSubData(surface->texture, x, y, width, height, bpp, data); |
148 | } |
149 | void* SG_CALL__cdecl sgSurfaceGetData(SGSurface* surface) |
150 | { |
151 | return sgTextureGetData(surface->texture); |
152 | } |
153 | void SG_CALL__cdecl sgSurfaceFreeData(void* data) |
154 | { |
155 | sgTextureFreeData(data); |
156 | } |
157 | |
158 | void SG_CALL__cdecl sgSurfaceDrawRads3f2f2f1f(SGSurface* surface, float x, float y, float z, float xscale, float yscale, float xoffset, float yoffset, float angle) |
159 | { |
160 | sgTextureDrawRads3f2f2f1f(surface->texture, x, y, z, xscale, yscale, xoffset, yoffset, angle); |
161 | } |
162 | void SG_CALL__cdecl sgSurfaceDrawDegs3f2f2f1f(SGSurface* surface, float x, float y, float z, float xscale, float yscale, float xoffset, float yoffset, float angle) |
163 | { |
164 | sgSurfaceDrawRads3f2f2f1f(surface, x, y, z, xscale, yscale, xoffset, yoffset, angle * SG_PI3.14159265358979323846 / 180.0); |
165 | } |
166 | void SG_CALL__cdecl sgSurfaceDrawRads2f2f2f1f(SGSurface* surface, float x, float y, float xscale, float yscale, float xoffset, float yoffset, float angle) |
167 | { |
168 | sgSurfaceDrawRads3f2f2f1f(surface, x, y, 0.0, xscale, yscale, xoffset, yoffset, angle); |
169 | } |
170 | void SG_CALL__cdecl sgSurfaceDrawDegs2f2f2f1f(SGSurface* surface, float x, float y, float xscale, float yscale, float xoffset, float yoffset, float angle) |
171 | { |
172 | sgSurfaceDrawDegs3f2f2f1f(surface, x, y, 0.0, xscale, yscale, xoffset, yoffset, angle); |
173 | } |
174 | void SG_CALL__cdecl sgSurfaceDrawRads3f2f1f(SGSurface* surface, float x, float y, float z, float xscale, float yscale, float angle) |
175 | { |
176 | sgSurfaceDrawRads3f2f2f1f(surface, x, y, z, xscale, yscale, 0.0, 0.0, angle); |
177 | } |
178 | void SG_CALL__cdecl sgSurfaceDrawDegs3f2f1f(SGSurface* surface, float x, float y, float z, float xscale, float yscale, float angle) |
179 | { |
180 | sgSurfaceDrawDegs3f2f2f1f(surface, x, y, z, xscale, yscale, 0.0, 0.0, angle); |
181 | } |
182 | void SG_CALL__cdecl sgSurfaceDrawRads2f2f1f(SGSurface* surface, float x, float y, float xscale, float yscale, float angle) |
183 | { |
184 | sgSurfaceDrawRads3f2f2f1f(surface, x, y, 0.0, xscale, yscale, 0.0, 0.0, angle); |
185 | } |
186 | void SG_CALL__cdecl sgSurfaceDrawDegs2f2f1f(SGSurface* surface, float x, float y, float xscale, float yscale, float angle) |
187 | { |
188 | sgSurfaceDrawDegs3f2f2f1f(surface, x, y, 0.0, xscale, yscale, 0.0, 0.0, angle); |
189 | } |
190 | void SG_CALL__cdecl sgSurfaceDrawRads3f1f(SGSurface* surface, float x, float y, float z, float angle) |
191 | { |
192 | sgSurfaceDrawRads3f2f2f1f(surface, x, y, z, 1.0, 1.0, 0.0, 0.0, angle); |
193 | } |
194 | void SG_CALL__cdecl sgSurfaceDrawDegs3f1f(SGSurface* surface, float x, float y, float z, float angle) |
195 | { |
196 | sgSurfaceDrawDegs3f2f2f1f(surface, x, y, z, 1.0, 1.0, 0.0, 0.0, angle); |
197 | } |
198 | void SG_CALL__cdecl sgSurfaceDrawRads2f1f(SGSurface* surface, float x, float y, float angle) |
199 | { |
200 | sgSurfaceDrawRads3f2f2f1f(surface, x, y, 0.0, 1.0, 1.0, 0.0, 0.0, angle); |
201 | } |
202 | void SG_CALL__cdecl sgSurfaceDrawDegs2f1f(SGSurface* surface, float x, float y, float angle) |
203 | { |
204 | sgSurfaceDrawDegs3f2f2f1f(surface, x, y, 0.0, 1.0, 1.0, 0.0, 0.0, angle); |
205 | } |
206 | void SG_CALL__cdecl sgSurfaceDraw3f2f2f(SGSurface* surface, float x, float y, float z, float xscale, float yscale, float xoffset, float yoffset) |
207 | { |
208 | sgSurfaceDrawRads3f2f2f1f(surface, x, y, z, xscale, yscale, xoffset, yoffset, 0.0); |
209 | } |
210 | void SG_CALL__cdecl sgSurfaceDraw2f2f2f(SGSurface* surface, float x, float y, float xscale, float yscale, float xoffset, float yoffset) |
211 | { |
212 | sgSurfaceDrawRads3f2f2f1f(surface, x, y, 0.0, xscale, yscale, xoffset, yoffset, 0.0); |
213 | } |
214 | void SG_CALL__cdecl sgSurfaceDraw3f2f(SGSurface* surface, float x, float y, float z, float xscale, float yscale) |
215 | { |
216 | sgSurfaceDrawRads3f2f2f1f(surface, x, y, z, xscale, yscale, 0.0, 0.0, 0.0); |
217 | } |
218 | void SG_CALL__cdecl sgSurfaceDraw2f2f(SGSurface* surface, float x, float y, float xscale, float yscale) |
219 | { |
220 | sgSurfaceDrawRads3f2f2f1f(surface, x, y, 0.0, xscale, yscale, 0.0, 0.0, 0.0); |
221 | } |
222 | void SG_CALL__cdecl sgSurfaceDraw3f(SGSurface* surface, float x, float y, float z) |
223 | { |
224 | sgSurfaceDrawRads3f2f2f1f(surface, x, y, z, 1.0, 1.0, 0.0, 0.0, 0.0); |
225 | } |
226 | void SG_CALL__cdecl sgSurfaceDraw2f(SGSurface* surface, float x, float y) |
227 | { |
228 | sgSurfaceDrawRads3f2f2f1f(surface, x, y, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0); |
229 | } |
230 | void SG_CALL__cdecl sgSurfaceDraw(SGSurface* surface) |
231 | { |
232 | sgSurfaceDrawRads3f2f2f1f(surface, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f); |
233 | } |
234 | |
235 | void SG_CALL__cdecl sgSurfaceTarget(SGSurface* surface) |
236 | { |
237 | glPushAttrib(GL_VIEWPORT_BIT0x00000800); |
238 | glViewport(0, 0, surface->texture->width, surface->texture->height); |
239 | glMatrixMode(GL_PROJECTION0x1701); |
240 | glPushMatrix(); |
241 | glLoadIdentity(); |
242 | glOrtho(0, surface->texture->width, surface->texture->height, 0, 127, -128); |
243 | glMatrixMode(GL_MODELVIEW0x1700); |
244 | |
245 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT0x8D40, GLFBO(surface)(*(GLuint*)(surface)->fboid)); |
246 | } |
247 | void SG_CALL__cdecl sgSurfaceUntarget(SGSurface* surface) |
248 | { |
249 | glMatrixMode(GL_PROJECTION0x1701); |
250 | glPopMatrix(); |
251 | glMatrixMode(GL_MODELVIEW0x1700); |
252 | glPopAttrib(); |
253 | |
254 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT0x8D40, 0); |
255 | } |
256 | void SG_CALL__cdecl sgSurfaceClear4f(SGSurface* surface, float r, float g, float b, float a) |
257 | { |
258 | sgSurfaceTarget(surface); |
259 | sgDrawClear4f(r, g, b, a); |
260 | sgSurfaceUntarget(surface); |
261 | } |
262 | void SG_CALL__cdecl sgSurfaceClear3f(SGSurface* surface, float r, float g, float b) |
263 | { |
264 | sgSurfaceClear4f(surface, r, g, b, 1.0f); |
265 | } |
266 | void SG_CALL__cdecl sgSurfaceClear2f(SGSurface* surface, float g, float a) |
267 | { |
268 | sgSurfaceClear4f(surface, g, g, g, a); |
269 | } |
270 | void SG_CALL__cdecl sgSurfaceClear1f(SGSurface* surface, float g) |
271 | { |
272 | sgSurfaceClear4f(surface, g, g, g, 1.0f); |
273 | } |
274 | void SG_CALL__cdecl sgSurfaceClear4ub(SGSurface* surface, SGubyte r, SGubyte g, SGubyte b, SGubyte a) |
275 | { |
276 | sgSurfaceClear4f(surface, r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f); |
277 | } |
278 | void SG_CALL__cdecl sgSurfaceClear3ub(SGSurface* surface, SGubyte r, SGubyte g, SGubyte b) |
279 | { |
280 | sgSurfaceClear4f(surface, r / 255.0f, g / 255.0f, b / 255.0f, 1.0f); |
281 | } |
282 | void SG_CALL__cdecl sgSurfaceClear2ub(SGSurface* surface, SGubyte g, SGubyte a) |
283 | { |
284 | sgSurfaceClear4f(surface, g / 255.0f, g / 255.0f, g / 255.0f, a / 255.0f); |
285 | } |
286 | void SG_CALL__cdecl sgSurfaceClear1ub(SGSurface* surface, SGubyte g) |
287 | { |
288 | sgSurfaceClear4f(surface, g / 255.0f, g / 255.0f, g / 255.0f, 1.0f); |
289 | } |
290 | void SG_CALL__cdecl sgSurfaceClear(SGSurface* surface) |
291 | { |
292 | sgSurfaceClear4f(surface, 0.0f, 0.0f, 0.0f, 0.0f); |
293 | } |
294 | |
295 | void SG_CALL__cdecl sgSurfaceGetSize(SGSurface* surface, SGuint* width, SGuint* height) |
296 | { |
297 | sgTextureGetSize(surface->texture, width, height); |
298 | } |
299 | SGuint SG_CALL__cdecl sgSurfaceGetWidth(SGSurface* surface) |
300 | { |
301 | return sgTextureGetWidth(surface->texture); |
302 | } |
303 | SGuint SG_CALL__cdecl sgSurfaceGetHeight(SGSurface* surface) |
304 | { |
305 | return sgTextureGetHeight(surface->texture); |
306 | } |
307 | SGenum SG_CALL__cdecl sgSurfaceGetBPP(SGSurface* surface) |
308 | { |
309 | return sgTextureGetBPP(surface->texture); |
310 | } |