this repo has no description
1#include "SDL.h"
2#include "SDL_gpu.h"
3#include <math.h>
4#include "common.h"
5#include <stdlib.h>
6
7
8void set_vertex(float* vertex_array, unsigned int vertex_index, unsigned short* index_array, unsigned int index_array_index, float x, float y, float s, float t, float r, float g, float b, float a)
9{
10 unsigned int n = vertex_index*8;
11 vertex_array[n++] = x;
12 vertex_array[n++] = y;
13 vertex_array[n++] = s;
14 vertex_array[n++] = t;
15 vertex_array[n++] = r;
16 vertex_array[n++] = g;
17 vertex_array[n++] = b;
18 vertex_array[n++] = a;
19 index_array[index_array_index++] = vertex_index;
20}
21
22void set_index(unsigned short* index_array, unsigned int index_array_index, unsigned short index)
23{
24 index_array[index_array_index] = index;
25}
26
27void polygon_blit(GPU_Image* image, GPU_Target* target, unsigned int num_vertices, float* vertices_xy, float tex_x, float tex_y, float scale_x, float scale_y)
28{
29 GPU_WrapEnum wrap_x, wrap_y;
30 float* vertices;
31 unsigned short* indices;
32 unsigned short vcount;
33 unsigned int icount;
34
35 int numSegments;
36 float r, g, b, a;
37 unsigned int i;
38 unsigned short last_index;
39
40 if(num_vertices < 3)
41 return;
42
43 wrap_x = image->wrap_mode_x;
44 wrap_y = image->wrap_mode_y;
45 GPU_SetWrapMode(image, GPU_WRAP_REPEAT, GPU_WRAP_REPEAT);
46
47 vertices = (float*)malloc(8*num_vertices*sizeof(float));
48 indices = (unsigned short*)malloc((3 + (num_vertices-3)*3)*sizeof(unsigned short));
49 vcount = 0;
50 icount = 0;
51
52 numSegments = 2*num_vertices;
53 r = g = b = a = 255.0f;
54
55 // Using a fan of triangles assumes that the polygon is convex
56 i = 0;
57
58 // First triangle
59 set_vertex(vertices, vcount++, indices, icount++, vertices_xy[i], vertices_xy[i+1], (vertices_xy[i] - tex_x)*scale_x, (vertices_xy[i+1] - tex_y)*scale_y, r, g, b, a);
60 i+=2;
61 set_vertex(vertices, vcount++, indices, icount++, vertices_xy[i], vertices_xy[i+1], (vertices_xy[i] - tex_x)*scale_x, (vertices_xy[i+1] - tex_y)*scale_y, r, g, b, a);
62 i+=2;
63 set_vertex(vertices, vcount++, indices, icount++, vertices_xy[i], vertices_xy[i+1], (vertices_xy[i] - tex_x)*scale_x, (vertices_xy[i+1] - tex_y)*scale_y, r, g, b, a);
64 i+=2;
65
66 last_index = 2;
67
68 while(i < numSegments)
69 {
70 set_index(indices, icount++, 0); // Start from the first vertex
71 set_index(indices, icount++, last_index); // Double the last one
72 set_vertex(vertices, vcount++, indices, icount++, vertices_xy[i], vertices_xy[i+1], (vertices_xy[i] - tex_x)*scale_x, (vertices_xy[i+1] - tex_y)*scale_y, r, g, b, a);
73 i+=2;
74 last_index++;
75 }
76
77 // Do the rendering
78 GPU_TriangleBatch(image, target, vcount, vertices, icount, indices, GPU_NONE);
79
80 // Restore wrap mode
81 GPU_SetWrapMode(image, wrap_x, wrap_y);
82
83 free(indices);
84 free(vertices);
85}
86
87int main(int argc, char* argv[])
88{
89 GPU_Target* screen;
90
91 printRenderers();
92
93 screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS);
94 if(screen == NULL)
95 return -1;
96
97 printCurrentRenderer();
98
99 {
100 Uint32 startTime;
101 long frameCount;
102 Uint8 done;
103 SDL_Event event;
104
105 GPU_Image* img;
106 int i;
107 #define NUM_POLYS 4
108 int pn[NUM_POLYS];
109 float* pv[NUM_POLYS];
110
111 startTime = SDL_GetTicks();
112 frameCount = 0;
113
114
115 img = GPU_LoadImage("data/test4.bmp");
116
117 for(i = 0; i < NUM_POLYS; i++)
118 {
119 int j;
120 pn[i] = rand()%8;
121 pv[i] = (float*)malloc(2*pn[i]*sizeof(float));
122 for(j = 0; j < pn[i]*2; j+=2)
123 {
124 pv[i][j] = rand()%screen->w;
125 pv[i][j+1] = rand()%screen->h;
126 }
127 }
128
129
130 done = 0;
131 while(!done)
132 {
133 while(SDL_PollEvent(&event))
134 {
135 if(event.type == SDL_QUIT)
136 done = 1;
137 else if(event.type == SDL_KEYDOWN)
138 {
139 if(event.key.keysym.sym == SDLK_ESCAPE)
140 done = 1;
141 }
142 }
143
144 GPU_Clear(screen);
145
146 for(i = 0; i < NUM_POLYS; i++)
147 {
148 polygon_blit(img, screen, pn[i], pv[i], pv[i][0], pv[i][1], 1.0f, 1.0f);
149 }
150
151
152 GPU_Flip(screen);
153
154 frameCount++;
155 if(frameCount%500 == 0)
156 printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime));
157 }
158
159 printf("Average FPS: %.2f\n", 1000.0f*frameCount/(SDL_GetTicks() - startTime));
160
161 for(i = 0; i < NUM_POLYS; i++)
162 {
163 free(pv[i]);
164 }
165 }
166
167 GPU_Quit();
168
169 return 0;
170}
171
172