engine: reject mbf21 and shit24 wads. there is no way to know if it is safe to ignore...
[k8vavoom.git] / vccrun / demos / simple_gfx_demo.vc
blobdc07287e0dcd645b81a6ef5b09e932dba1ada66a
1 import 'GLVideo';
2 import 'SimpleDraw';
4 class Main : Object;
6 SimpleDraw drawer;
8 TVec boxPoint;
9 bool leftPressed, rightPressed;
12 // ////////////////////////////////////////////////////////////////////////// //
13 // this is called when vccrun need to refresh/redraw the screen
14 void onDraw () {
15   GLVideo.depthTest = false;
16   GLVideo.glSetup2D();
17   GLVideo.glSetColor(1, 1, 1);
19   GLVideo.clearScreen();
21   GLVideo.color = 0xff_ff_ff;
22   drawer.drawDoomBox(boxPoint, 16);
26 // ////////////////////////////////////////////////////////////////////////// //
27 // this is called on each event
28 void onEvent (ref event_t evt) {
29   if (evt.type == ev_closequery) { GLVideo.requestQuit(); writeln("exiting (", evt.data1, ")"); return; }
30   if (evt.type == ev_keyup && ((evt.keycode == K_q && evt.bCtrl) || evt.keycode == K_ESCAPE)) {
31     // call this to stop event loop
32     GLVideo.requestQuit();
33     return;
34   }
36   if (evt.type == ev_keydown || evt.type == ev_keyup) {
37     switch (evt.keycode) {
38       case K_LEFTARROW:
39       case K_PAD4:
40       case K_BUTTON_DPAD_LEFT:
41         leftPressed = (evt.type == ev_keydown);
42         break;
44       case K_RIGHTARROW:
45       case K_PAD6:
46       case K_BUTTON_DPAD_RIGHT:
47         rightPressed = (evt.type == ev_keydown);
48         break;
49     }
50   }
52   if (evt.type == ev_joystick) {
53     print("jidx=%d; dx=%d; dy=%d", evt.joyidx, evt.dx, evt.dy);
54   }
58 // ////////////////////////////////////////////////////////////////////////// //
59 // this is called on each new frame (kind of timer)
60 void onNewFrame () {
61   // do frame logic
62   if (leftPressed && boxPoint.x > 0) boxPoint.x -= 1;
63   if (rightPressed && boxPoint.x < 800) boxPoint.x += 1;
64   // request screen refresh
65   // do not call `onDraw()` manuslly, use this API instead!
66   GLVideo.requestRefresh();
70 void main () {
71   drawer = SpawnObject(SimpleDraw);
72   scope(exit) delete drawer;
74   drawer.scaleX = 1;
75   drawer.scaleY = 1;
76   drawer.offsetX = 0;
77   drawer.offsetY = 0;
79   boxPoint = vector(400, 300);
81   // milliseconds in one frame
82   // `onNewFrame()` will be called with roughly this interval
83   GLVideo.frameTime = int(1000/30); // 30 FPS
85   GLVideo.openScreen("Simple Gfx Demo", 800, 600);
86   scope(exit) GLVideo.closeScreen();
87   //GLVideo.swapInterval = 0; // turn off VSync
88   GLVideo.swapInterval = 1; // turn on VSync
89   GLVideo.runEventLoop();