PS2GL
OpenGL*-like API for the PS2
Loading...
Searching...
No Matches
ps2glut.cpp
1/* Copyright (C) 2000,2001,2002 Sony Computer Entertainment America
2
3 This file is subject to the terms and conditions of the GNU Lesser
4 General Public License Version 2.1. See the file "COPYING" in the
5 main directory of this archive for more details. */
6
7/* libc */
8#include <stdio.h>
9#include <stdlib.h>
10
11/* ps2sdk */
12#include "kernel.h"
13#include "graph.h"
14#include "pads.h"
15
16/* GL */
17#include "GL/glut.h"
18#include "GL/ps2gl.h"
19
20/* ps2stuff */
21#include "ps2s/core.h"
22#include "ps2s/displayenv.h"
23#include "ps2s/drawenv.h"
24#include "ps2s/gs.h"
25#include "ps2s/timer.h"
26
27/* ps2gl */
28#include "ps2gl/debug.h"
29#include "ps2gl/displaycontext.h"
30#include "ps2gl/drawcontext.h"
31#include "ps2gl/glcontext.h"
32
33
34DISABLE_EXTRA_TIMERS_FUNCTIONS();
35
36
37/********************************************
38 * some function pointer types
39 */
40
41typedef void (*tFunctionPtr_ii)(int, int);
42typedef void (*tFunctionPtr_ucii)(unsigned char, int, int);
43typedef void (*tFunctionPtr_iii)(int, int, int);
44typedef void (*tFunctionPtr)(void);
45typedef void (*tFunctionPtr_i)(int);
46
47/********************************************
48 * function prototypes
49 */
50
51static void normal_main_loop();
52static void initGsMemory();
53static void do_keys();
54
55/********************************************
56 * local data
57 */
58
59tFunctionPtr DisplayFunc = NULL;
60tFunctionPtr_ii ReshapeFunc = NULL;
61tFunctionPtr_ucii KeyboardFunc = NULL;
62tFunctionPtr_i VisibilityFunc = NULL;
63tFunctionPtr IdleFunc = NULL;
64tFunctionPtr_iii SpecialFunc = NULL;
65
66static CEETimer* Timer0;
67
68static bool printTimes = false;
69
70/********************************************
71 * macros
72 */
73
74/********************************************
75 * glut implementation (loosely speaking..)
76 */
77
104void glutInit(int* argcp, char** argv)
105{
106 Pads::Init();
107
108 // does the ps2gl library need to be initialized?
109
111 // reset the machine
112 // sceDevVif0Reset();
113 // sceDevVu0Reset();
114 // sceDmaReset(1);
115 // sceGsResetPath();
116
117 // Reset the GIF. OSDSYS leaves PATH3 busy, that ends up having
118 // our PATH1/2 transfers ignored by the GIF.
119 *GIF::Registers::ctrl = 1;
120
121 // sceGsResetGraph(0, SCE_GS_INTERLACE, SCE_GS_NTSC, SCE_GS_FRAME);
122 SetGsCrt(1 /* interlaced */, 2 /* ntsc */, 1 /* frame */);
123
124 mWarn("ps2gl library has not been initialized by the user; using default values.");
125 int immBufferVertexSize = 64 * 1024;
126 pglInit(immBufferVertexSize, 1000);
127 }
128
129 // does gs memory need to be initialized?
130
131 if (!pglHasGsMemBeenInitted()) {
132 mWarn("GS memory has not been allocated by the user; using default values.");
133 initGsMemory();
134 }
135}
136
140void glutDisplayFunc(void (*func)(void))
141{
142 DisplayFunc = func;
143}
144
150void glutReshapeFunc(void (*func)(int width, int height))
151{
152 ReshapeFunc = func;
153}
154
160void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y))
161{
162 KeyboardFunc = func;
163}
164
170void glutVisibilityFunc(void (*func)(int state))
171{
172 VisibilityFunc = func;
173}
174
179void glutIdleFunc(void (*func)(void))
180{
181 IdleFunc = func;
182}
183
189void glutSpecialFunc(void (*func)(int key, int x, int y))
190{
191 SpecialFunc = func;
192}
193
198void glutMainLoop(void)
199{
200 mErrorIf(DisplayFunc == NULL, "ps2glut: No display function!");
201
202 if (VisibilityFunc)
203 VisibilityFunc(GLUT_VISIBLE);
204
205 normal_main_loop();
206}
207
// glut_api
209
210void glutInitDisplayMode(unsigned int mode)
211{
212 mNotImplemented();
213}
214
215void glutInitWindowPosition(int x, int y)
216{
217 mNotImplemented();
218}
219
220void glutInitWindowSize(int x, int y)
221{
222 mNotImplemented();
223}
224
225int glutCreateWindow(const char* title)
226{
227 mNotImplemented();
228
229 return 1;
230}
231
232void glutPostRedisplay(void)
233{
234 // mNotImplemented( );
235}
236
237void glutSwapBuffers(void)
238{
239}
240
241int glutGet(GLenum type)
242{
243 mNotImplemented();
244 return 0;
245}
246
247void* pglutAllocDmaMem(unsigned int num_bytes)
248{
249 // don't really need memalign, but it's clearer..
250 return memalign(16, num_bytes);
251}
252
253void pglutFreeDmaMem(void* mem)
254{
255 free(mem);
256}
257
258/********************************************
259 * local function definitions
260 */
261
262void normal_main_loop()
263{
264 bool firstTime = true;
265
266 // init the timing system
267
268 Timer0 = new CEETimer(CEETimer::Timer0);
269 mInitTimers(Timer0, CEETimer::BusClock_256th);
270
271 if (ReshapeFunc)
272 ReshapeFunc(640, 448);
273
274 while (1) {
275 mUpdateTimers();
276
277 mStartTimer("frame time");
278
279 Pads::Read();
280
281 if (Pad0.WasPushed(Pads::kStart))
282 printTimes = true;
283
284 do_keys();
285
286 if (DisplayFunc) {
287 mStartTimer("DisplayFunc()");
288 pglBeginGeometry();
289 DisplayFunc();
290 pglEndGeometry();
291 mStopTimer("DisplayFunc()");
292 }
293
294 if (IdleFunc) {
295 mStartTimer("IdleFunc()");
296 IdleFunc();
297 mStopTimer("IdleFunc()");
298 }
299
300 mStartTimer("wait for vu1");
301 if (!firstTime)
302 pglFinishRenderingGeometry(PGL_DONT_FORCE_IMMEDIATE_STOP);
303 else
304 firstTime = false;
305 mStopTimer("wait for vu1");
306
307 mStopTimer("frame time");
308
309 if (printTimes) {
310 mDisplayTimers();
311 printTimes = false;
312
313 // pglPrintGsMemAllocation();
314 }
315
318 pglRenderGeometry();
319 }
320}
321
322void do_keys()
323{
324 static int frameCount = 0;
325
326 if (SpecialFunc) {
327 if (Pad0.WasPushed(Pads::kLeftUp)) {
328 SpecialFunc(GLUT_KEY_UP, 0, 0);
329 frameCount = 0;
330 }
331 if (Pad0.WasPushed(Pads::kLeftDown)) {
332 SpecialFunc(GLUT_KEY_DOWN, 0, 0);
333 frameCount = 0;
334 }
335 if (Pad0.WasPushed(Pads::kLeftRight)) {
336 SpecialFunc(GLUT_KEY_RIGHT, 0, 0);
337 frameCount = 0;
338 }
339 if (Pad0.WasPushed(Pads::kLeftLeft)) {
340 SpecialFunc(GLUT_KEY_LEFT, 0, 0);
341 frameCount = 0;
342 }
343
344 if (Pad0.WasPushed(Pads::kL1)) {
345 SpecialFunc(GLUT_KEY_HOME, 0, 0);
346 frameCount = 0;
347 }
348 if (Pad0.WasPushed(Pads::kL2)) {
349 SpecialFunc(GLUT_KEY_END, 0, 0);
350 frameCount = 0;
351 }
352
353 if (Pad0.WasPushed(Pads::kR1)) {
354 SpecialFunc(GLUT_KEY_PAGE_UP, 0, 0);
355 frameCount = 0;
356 }
357 if (Pad0.WasPushed(Pads::kR2)) {
358 SpecialFunc(GLUT_KEY_PAGE_DOWN, 0, 0);
359 frameCount = 0;
360 }
361 }
362
363 if (KeyboardFunc) {
364 if (Pad0.IsDown(Pads::kRightUp)) {
365 KeyboardFunc('8', 0, 0);
366 frameCount = 0;
367 }
368 if (Pad0.IsDown(Pads::kRightDown)) {
369 KeyboardFunc('2', 0, 0);
370 frameCount = 0;
371 }
372 if (Pad0.IsDown(Pads::kRightLeft)) {
373 KeyboardFunc('4', 0, 0);
374 frameCount = 0;
375 }
376 if (Pad0.IsDown(Pads::kRightRight)) {
377 KeyboardFunc('6', 0, 0);
378 frameCount = 0;
379 }
380 }
381
382 if (frameCount > 40 && frameCount % 3 == 0) {
383
384 if (SpecialFunc) {
385 if (Pad0.IsDown(Pads::kLeftUp)) {
386 SpecialFunc(GLUT_KEY_UP, 0, 0);
387 }
388 if (Pad0.IsDown(Pads::kLeftDown)) {
389 SpecialFunc(GLUT_KEY_DOWN, 0, 0);
390 }
391 if (Pad0.IsDown(Pads::kLeftRight)) {
392 SpecialFunc(GLUT_KEY_RIGHT, 0, 0);
393 }
394 if (Pad0.IsDown(Pads::kLeftLeft)) {
395 SpecialFunc(GLUT_KEY_LEFT, 0, 0);
396 }
397
398 if (Pad0.IsDown(Pads::kL1)) {
399 SpecialFunc(GLUT_KEY_HOME, 0, 0);
400 }
401 if (Pad0.IsDown(Pads::kL2)) {
402 SpecialFunc(GLUT_KEY_END, 0, 0);
403 }
404
405 if (Pad0.IsDown(Pads::kR1)) {
406 SpecialFunc(GLUT_KEY_PAGE_UP, 0, 0);
407 }
408 if (Pad0.IsDown(Pads::kR2)) {
409 SpecialFunc(GLUT_KEY_PAGE_DOWN, 0, 0);
410 }
411 }
412
413 if (KeyboardFunc) {
414 if (Pad0.IsDown(Pads::kRightUp)) {
415 KeyboardFunc('8', 0, 0);
416 }
417 if (Pad0.IsDown(Pads::kRightDown)) {
418 KeyboardFunc('2', 0, 0);
419 }
420 if (Pad0.IsDown(Pads::kRightLeft)) {
421 KeyboardFunc('4', 0, 0);
422 }
423 if (Pad0.IsDown(Pads::kRightRight)) {
424 KeyboardFunc('6', 0, 0);
425 }
426 }
427 }
428
429 frameCount++;
430}
431
432static void
433initGsMemory()
434{
435 // frame and depth buffer
436 pgl_slot_handle_t frame_slot_0, frame_slot_1, depth_slot;
437 frame_slot_0 = pglAddGsMemSlot(0, 70, GS::kPsm32);
438 frame_slot_1 = pglAddGsMemSlot(70, 70, GS::kPsm32);
439 depth_slot = pglAddGsMemSlot(140, 70, GS::kPsmz24);
440 // lock these slots so that they aren't allocated by the memory manager
441 pglLockGsMemSlot(frame_slot_0);
442 pglLockGsMemSlot(frame_slot_1);
443 pglLockGsMemSlot(depth_slot);
444
445 // create gs memory area objects to use for frame and depth buffers
446 pgl_area_handle_t frame_area_0, frame_area_1, depth_area;
447 frame_area_0 = pglCreateGsMemArea(640, 224, GS::kPsm24);
448 frame_area_1 = pglCreateGsMemArea(640, 224, GS::kPsm24);
449 depth_area = pglCreateGsMemArea(640, 224, GS::kPsmz24);
450 // bind the areas to the slots we created above
451 pglBindGsMemAreaToSlot(frame_area_0, frame_slot_0);
452 pglBindGsMemAreaToSlot(frame_area_1, frame_slot_1);
453 pglBindGsMemAreaToSlot(depth_area, depth_slot);
454
455 // draw to the new areas...
456 pglSetDrawBuffers(PGL_INTERLACED, frame_area_0, frame_area_1, depth_area);
457 // ...and display from them
458 pglSetDisplayBuffers(PGL_INTERLACED, frame_area_0, frame_area_1);
459
460 // 32 bit
461
462 // a slot for fonts (probably)
463 pglAddGsMemSlot(210, 2, GS::kPsm8);
464
465 // 64x32
466 pglAddGsMemSlot(212, 1, GS::kPsm32);
467 pglAddGsMemSlot(213, 1, GS::kPsm32);
468 // 64x64
469 pglAddGsMemSlot(214, 2, GS::kPsm32);
470 pglAddGsMemSlot(216, 2, GS::kPsm32);
471 pglAddGsMemSlot(218, 2, GS::kPsm32);
472 pglAddGsMemSlot(220, 2, GS::kPsm32);
473 // 128x128
474 pglAddGsMemSlot(222, 8, GS::kPsm32);
475 pglAddGsMemSlot(230, 8, GS::kPsm32);
476 // 256x256
477 pglAddGsMemSlot(238, 32, GS::kPsm32);
478 pglAddGsMemSlot(270, 32, GS::kPsm32);
479 // 512x256
480 pglAddGsMemSlot(302, 64, GS::kPsm32);
481 pglAddGsMemSlot(366, 64, GS::kPsm32);
482
484}
void glutVisibilityFunc(void(*func)(int state))
Definition ps2glut.cpp:170
void glutSpecialFunc(void(*func)(int key, int x, int y))
Definition ps2glut.cpp:189
void glutReshapeFunc(void(*func)(int width, int height))
Definition ps2glut.cpp:150
void glutIdleFunc(void(*func)(void))
Definition ps2glut.cpp:179
void glutInit(int *argcp, char **argv)
Definition ps2glut.cpp:104
void glutMainLoop(void)
Definition ps2glut.cpp:198
void glutDisplayFunc(void(*func)(void))
Definition ps2glut.cpp:140
void glutKeyboardFunc(void(*func)(unsigned char key, int x, int y))
Definition ps2glut.cpp:160
void pglBindGsMemAreaToSlot(pgl_area_handle_t mem_area, pgl_slot_handle_t mem_slot)
Definition gsmemory.cpp:241
pgl_area_handle_t pglCreateGsMemArea(int width, int height, unsigned int pix_format)
Definition gsmemory.cpp:185
pgl_slot_handle_t pglAddGsMemSlot(int startingPage, int pageLength, unsigned int pixelMode)
Definition gsmemory.cpp:118
void pglLockGsMemSlot(pgl_slot_handle_t slot_handle)
Definition gsmemory.cpp:132
int pglHasGsMemBeenInitted(void)
Definition gsmemory.cpp:50
void pglPrintGsMemAllocation(void)
Definition gsmemory.cpp:40
void pglSetDisplayBuffers(int interlaced, pgl_area_handle_t frame0_mem, pgl_area_handle_t frame1_mem)
void pglSetDrawBuffers(int interlaced, pgl_area_handle_t frame0_mem, pgl_area_handle_t frame1_mem, pgl_area_handle_t depth_mem)
void pglSwapBuffers(void)
int pglHasLibraryBeenInitted(void)
void pglWaitForVSync(void)
int pglInit(int immBufferVertexSize, int immDrawBufferQwordSize)