| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- MODULE event_loop ;
- (*
- m2SDL example: event loop until window close or 5 seconds.
- Uses the opaque SDLEvents subset: PollEvent over a raw buffer,
- first CARDINAL of SDL_Event is the Uint32 type - offset 0 on
- little and big endian alike.
- *)
- FROM SYSTEM IMPORT ADDRESS, ADR;
- FROM SDL2 IMPORT SDL_Init, SDL_Quit, SDL_Delay,
- SDL_GetTicks64, SDLInitVideo, SDLInitEvents;
- FROM SDLVideo IMPORT SDL_CreateWindow, SDL_DestroyWindow,
- WindowShown, WindowPosUndefined;
- FROM SDLEvents IMPORT SDL_PollEvent, EventQuit, EventBufferBytes;
- FROM libc IMPORT printf;
- VAR
- win: ADDRESS;
- buf: ARRAY [0..EventBufferBytes-1] OF CHAR;
- evType: CARDINAL;
- start, now: LONGCARD;
- quit: BOOLEAN;
- BEGIN
- IF SDL_Init(SDLInitVideo + SDLInitEvents) # 0 THEN
- printf("SDL_Init failed\n");
- HALT(1)
- END;
- win := SDL_CreateWindow("Close me, or wait 5s",
- WindowPosUndefined, WindowPosUndefined,
- 640, 480, WindowShown);
- IF win = NIL THEN
- printf("CreateWindow failed\n");
- SDL_Quit;
- HALT(1)
- END;
- quit := FALSE;
- start := SDL_GetTicks64();
- WHILE NOT quit DO
- WHILE SDL_PollEvent(ADR(buf)) # 0 DO
- evType := 0;
- evType := VAL(CARDINAL, ORD(buf[0]))
- + VAL(CARDINAL, ORD(buf[1])) * 256
- + VAL(CARDINAL, ORD(buf[2])) * 65536;
- IF evType = EventQuit THEN quit := TRUE END
- END;
- now := SDL_GetTicks64();
- IF now - start > 5000 THEN quit := TRUE END;
- SDL_Delay(16)
- END;
- SDL_DestroyWindow(win);
- SDL_Quit;
- printf("bye\n")
- END event_loop.
|