-
Notifications
You must be signed in to change notification settings - Fork 0
/
deviltest.cpp
256 lines (210 loc) · 6.83 KB
/
deviltest.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <IwGx.h>
#include <s3eKeyboard.h>
#include <s3ePointer.h>
#include <IL/il.h>
CIwTexture* tex = NULL;
ILuint imgId = -1;
float x = 50.0f;
float y = 50.0f;
float w = 200.0f;
float h = 200.0f;
float framerate = 60.0f;
int frame = 1;
uint32 time = 0;
void Update(uint32 dt) // dt is in ms
{
time += dt;
int duration = ilGetInteger(IL_IMAGE_DURATION);
//IwTrace(ILTEST, ("dur=%d time=%d dt=%d", duration, time, dt));
if (duration > 0 && time > duration)
{
time -= duration;
if (ilGetInteger(IL_NUM_IMAGES) == 0) // no frame in queue, loop
frame = 1;
else
++frame;
ilBindImage(imgId);
ILboolean x = ilActiveImage(frame);
ilConvertImage(IL_BGRA, IL_UNSIGNED_BYTE);
int type = ilGetInteger(IL_IMAGE_FORMAT);
int width = ilGetInteger(IL_IMAGE_WIDTH);
int height = ilGetInteger(IL_IMAGE_HEIGHT);
int bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
CIwImage::Format format;
switch (type)
{
case IL_RGBA: format = CIwImage::ABGR_8888; break;
case IL_BGRA: format = CIwImage::ARGB_8888; break;
case IL_BGR: format = CIwImage::RGB_888; break;
default:
IwAssert(ILTEST, false);
}
tex->CopyFromBuffer(width, height, format, width*bpp, ilGetData(), NULL);
tex->ChangeTexels(tex->GetTexels(), format);
#if 0
delete tex;
tex = new CIwTexture();
tex->SetModifiable(true);
tex->SetMipMapping(false);
tex->CopyFromBuffer(width, height, format, width*bpp, ilGetData(), /*(uint16*)ilGetPalette()*/ NULL);
tex->Upload();
#endif
//ILenum e = ilGetError();
//IwTrace(ILTEST, ("w=%d h=%d bpp=%d", width, height, bpp));
//IwTrace(ILTEST, ("frame=%d numimages=%d imageduration=%d", frame, ilGetInteger(IL_NUM_IMAGES), ilGetInteger(IL_IMAGE_DURATION)));
}
}
void Render()
{
CIwColour usedColor;
usedColor.Set(255, 255, 255, 255);
CIwColour *colors = IW_GX_ALLOC(CIwColour, 4);
colors[0] = usedColor;
colors[1] = usedColor;
colors[2] = usedColor;
colors[3] = usedColor;
CIwFVec2 *verts2 = IW_GX_ALLOC(CIwFVec2, 4);
verts2[0].x = x;
verts2[0].y = y;
verts2[1].x = verts2[0].x;
verts2[1].y = y + h;
verts2[2].x = x + w;
verts2[2].y = verts2[1].y;
verts2[3].x = verts2[2].x;
verts2[3].y = verts2[0].y;
CIwFVec2 *uvs = IW_GX_ALLOC(CIwFVec2, 4);
uvs[0].x = 0.0f;
uvs[0].y = 0.0f;
uvs[1].x = uvs[0].x;
uvs[1].y = 1.0f;
uvs[2].x = 1.0f;
uvs[2].y = uvs[1].y;
uvs[3].x = uvs[2].x;
uvs[3].y = uvs[0].y;
IwGxSetUVStream(uvs);
IwGxSetVertStreamScreenSpace(verts2, 4);
IwGxSetColStream(colors, 4);
IwGxSetNormStream(NULL);
CIwMaterial* mat = IW_GX_ALLOC_MATERIAL();
mat->SetCullMode(CIwMaterial::CULL_NONE);
mat->SetTexture(tex);
mat->SetDepthWriteMode(CIwMaterial::DEPTH_WRITE_DISABLED);
mat->SetAlphaMode(CIwMaterial::ALPHA_BLEND);
IwGxSetMaterial(mat);
IwGxDrawPrims(IW_GX_QUAD_LIST, NULL, 4);
IwGxPrintFrameRate(10, 10);
}
void HandleDevILErrors()
{
ILenum error = ilGetError();
if (error != IL_NO_ERROR) {
do {
printf("\n\n%d\n", error);
} while ((error = ilGetError()));
exit(1);
}
}
void LoadTexture()
{
//const char* FileName = "gradient.png";
const char* FileName = "bart.gif";
//const char* FileName = "pika.gif";
//const char* FileName = "alphatest.png";
// Needed to initialize DevIL.
ilInit();
// GL cannot use palettes anyway, so convert early.
//ilEnable(IL_CONV_PAL);
// Generate the main image name to use.
ilGenImages(1, &imgId);
// Bind this image name.
ilBindImage(imgId);
// Loads the image specified by File into the ImgId image.
if (!ilLoadImage(FileName)) {
HandleDevILErrors();
}
// Make sure the window is in the same proportions as the image.
// Generate the appropriate width x height less than or equal to MAX_X x MAX_Y.
// Instead of just clipping Width x Height to MAX_X x MAX_Y, we scale to
// an appropriate size, so the image isn't stretched/squished.
int width = ilGetInteger(IL_IMAGE_WIDTH);
int height = ilGetInteger(IL_IMAGE_HEIGHT);
IwTrace(ILTEST, ("%s: %d x %d (%d images)", FileName, width, height, ilGetInteger(IL_NUM_IMAGES)));
//ilLoadL(IL_TGA, Lump, Size);
//free(Lump);
}
void ReleaseTexture()
{
// We're done with our image, so we go ahead and delete it.
ilDeleteImages(1, &imgId);
ilShutDown();
}
int main()
{
IwGxInit();
IwGxSetOrtho(0, IwGxGetDisplayWidth(), 0, IwGxGetDisplayHeight(), 1.0f, 10.0f);
LoadTexture();
tex = new CIwTexture();
tex->SetModifiable(true);
tex->SetMipMapping(false);
//tex->LoadFromFile("gradient.png");
int ttype = ilGetInteger(IL_IMAGE_FORMAT);
ilConvertImage(IL_BGRA, IL_UNSIGNED_BYTE);
int width = ilGetInteger(IL_IMAGE_WIDTH);
int height = ilGetInteger(IL_IMAGE_HEIGHT);
int bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);
int type = ilGetInteger(IL_IMAGE_FORMAT);
CIwImage::Format format;
switch (type)
{
case IL_RGBA: format = CIwImage::ABGR_8888; break;
case IL_BGRA: format = CIwImage::ARGB_8888; break;
case IL_BGR: format = CIwImage::RGB_888; break;
default:
IwAssert(ILTEST, false);
}
/*
#define IL_COLOUR_INDEX 0x1900
#define IL_COLOR_INDEX 0x1900
#define IL_ALPHA 0x1906
#define IL_RGB 0x1907
#define IL_RGBA 0x1908
#define IL_BGR 0x80E0
#define IL_BGRA 0x80E1
#define IL_LUMINANCE 0x1909
#define IL_LUMINANCE_ALPHA 0x190A
*/
tex->CopyFromBuffer(width, height, format, width*bpp, ilGetData(), /*(uint16*)ilGetPalette()*/ NULL);
tex->Upload();
IwGxSetColClear(0, 0, 0, 255);
uint64 currentTime = s3eTimerGetMs();
while (!s3eDeviceCheckQuitRequest())
{
uint64 newTime = s3eTimerGetMs();
int32 timePassed = (int32)(newTime - currentTime);
int32 oneFrameTime = 1000 / framerate;
if (timePassed >= oneFrameTime)
s3eDeviceYield(0);
else
while (timePassed < oneFrameTime)
{
s3eDeviceYield((int32)(oneFrameTime - timePassed));
newTime = s3eTimerGetMs();
timePassed = (int32)(newTime - currentTime);
}
//IwTrace(Jerky, ("passed ms %ld", timePassed));
currentTime = newTime;
// Prevent back-light to off state
s3eDeviceBacklightOn();
s3ePointerUpdate();
s3eKeyboardUpdate();
Update(timePassed);
IwGxClear(IW_GX_COLOUR_BUFFER_F | IW_GX_DEPTH_BUFFER_F);
Render();
IwGxFlush();
IwGxSwapBuffers();
}
delete tex;
ReleaseTexture();
IwGxTerminate();
return 0;
}