|
| 1 | +#include "sdl.ceu" |
| 2 | + |
| 3 | +native do |
| 4 | + ##include <unistd.h> |
| 5 | + ##include <fcntl.h> |
| 6 | +end |
| 7 | + |
| 8 | +input void SDL_REDRAW; |
| 9 | +input void SDL_QUIT; |
| 10 | +input int SDL_DT; |
| 11 | +input _SDL_KeyboardEvent&& SDL_KEYDOWN; |
| 12 | +input _SDL_KeyboardEvent&& SDL_KEYUP; |
| 13 | +input _SDL_MouseButtonEvent&& SDL_MOUSEBUTTONDOWN; |
| 14 | +event (_SDL_Rect,int) collideTime; |
| 15 | + |
| 16 | +input char SERIAL; |
| 17 | + |
| 18 | +var _SDL_Window&? window; |
| 19 | + finalize |
| 20 | + window = &_SDL_CreateWindow("Game", |
| 21 | + _SDL_WINDOWPOS_UNDEFINED, |
| 22 | + _SDL_WINDOWPOS_UNDEFINED, |
| 23 | + 640, 480, _SDL_WINDOW_SHOWN); |
| 24 | + with |
| 25 | + _SDL_DestroyWindow(&&window!); |
| 26 | + end |
| 27 | + |
| 28 | +var _SDL_Renderer&? renderer; |
| 29 | + finalize |
| 30 | + renderer = &_SDL_CreateRenderer(&&window!, -1, 0); |
| 31 | + with |
| 32 | + _SDL_DestroyRenderer(&&renderer!); |
| 33 | + end |
| 34 | + |
| 35 | + |
| 36 | +class Player with |
| 37 | + var _SDL_Renderer& ren; |
| 38 | + var int x = 0; |
| 39 | + var int y = 0; |
| 40 | + var _SDL_Rect r = _SDL_Rect(x,y, 20,50); |
| 41 | + var int joystick = false; |
| 42 | + event void playerHit; |
| 43 | +do |
| 44 | + var int life = 3; |
| 45 | + var int vx = 0; |
| 46 | + var int vy = 0; |
| 47 | + par do |
| 48 | + every dt in SDL_DT do |
| 49 | + x = x + vx*dt; |
| 50 | + y = y + vy*dt; |
| 51 | + if this.x > 640000 then |
| 52 | + this.x = 0; |
| 53 | + end |
| 54 | + if this.x < 0 then |
| 55 | + this.x = 640000; |
| 56 | + end |
| 57 | + end |
| 58 | + with //keyboard controls |
| 59 | + par do |
| 60 | + loop do |
| 61 | + var _SDL_KeyboardEvent&& key; |
| 62 | + key = await SDL_KEYDOWN until key:keysym.sym==_SDLK_LEFT; |
| 63 | + vx = vx - 100; |
| 64 | + key = await SDL_KEYUP until key:keysym.sym==_SDLK_LEFT; |
| 65 | + vx = vx + 100; |
| 66 | + end |
| 67 | + with |
| 68 | + loop do |
| 69 | + var _SDL_KeyboardEvent&& key; |
| 70 | + key = await SDL_KEYDOWN until key:keysym.sym==_SDLK_RIGHT; |
| 71 | + vx = 100 + vx; |
| 72 | + key = await SDL_KEYUP until key:keysym.sym==_SDLK_RIGHT; |
| 73 | + vx = vx - 100 ; |
| 74 | + end |
| 75 | + with |
| 76 | + loop do |
| 77 | + var _SDL_KeyboardEvent&& key; |
| 78 | + key = await SDL_KEYDOWN until key:keysym.sym==_SDLK_UP; |
| 79 | + _printf("fire %d %d\n",x,y); |
| 80 | + end |
| 81 | + with //arduino controls |
| 82 | + par do |
| 83 | + var int orvx = vx; |
| 84 | + var int rightvx = vx + 100; |
| 85 | + var int leftvx = vx - 100; |
| 86 | + var char lastserial = 0; |
| 87 | + loop do |
| 88 | + var char c = await SERIAL; |
| 89 | + _printf("%c\n",c); |
| 90 | + if(c != lastserial) then |
| 91 | + if(c == 'W') then |
| 92 | + _printf("BEAM\n"); |
| 93 | + end |
| 94 | + if (c == 'D') then |
| 95 | + vx = rightvx; |
| 96 | + // _printf("Right\n"); |
| 97 | + end |
| 98 | + if(c == 'A') then |
| 99 | + vx = leftvx; |
| 100 | + // _printf("Left\n"); |
| 101 | + end |
| 102 | + if(c == '0') then |
| 103 | + vx = orvx; |
| 104 | + end |
| 105 | + lastserial = c; |
| 106 | + end |
| 107 | + end |
| 108 | + _printf("FINISHED reacting\n"); |
| 109 | + with |
| 110 | + |
| 111 | + var char c = 'x'; |
| 112 | + var int h =0; |
| 113 | + var int it = 0; |
| 114 | + async (h,c) do |
| 115 | + native @nohold _fscanf; |
| 116 | + native @nohold _fcntl; |
| 117 | + native @nohold _read; |
| 118 | + var int fd = _open("/dev/cu.usbmodem1421",_O_RDONLY); |
| 119 | + var int flags = _fcntl(fd,_F_GETFL,0); |
| 120 | + if(_fcntl(fd,_F_SETFL,flags | _O_NONBLOCK)) then |
| 121 | + _printf("ERROR NO CONTROLLER\n"); |
| 122 | + else |
| 123 | + _printf("Joystick ON\n"); |
| 124 | + loop do |
| 125 | + var int ret = _read(fd,&&c,1); |
| 126 | + if ret >= 0 then |
| 127 | + emit SERIAL => c; |
| 128 | + end |
| 129 | + end |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + _printf("FINISHED reading\n"); |
| 134 | + end |
| 135 | + end |
| 136 | + with |
| 137 | + every SDL_REDRAW do |
| 138 | + _SDL_SetRenderDrawColor(&&this.ren, |
| 139 | + 0x00,0x00,0x00,0x00); |
| 140 | + r.x = x / 1000; |
| 141 | + r.y = y / 1000; |
| 142 | + _SDL_RenderFillRect(&&this.ren, &&r); |
| 143 | + end |
| 144 | + with |
| 145 | + loop do |
| 146 | + await SDL_DT; |
| 147 | + await this.playerHit; |
| 148 | + this.life = this.life -1; |
| 149 | + _printf("life %d\n",this.life); |
| 150 | + if this.life <= 0 then |
| 151 | + _printf("GAME OVER\n"); |
| 152 | + _exit(0); |
| 153 | + escape true; |
| 154 | + end |
| 155 | + end |
| 156 | + end |
| 157 | +end |
| 158 | +par/or do |
| 159 | + every SDL_REDRAW do |
| 160 | + _SDL_SetRenderDrawColor(&&renderer!, |
| 161 | + 0xFF,0xFF,0xFF,0x00); |
| 162 | + _SDL_RenderFillRect(&&renderer!, null); |
| 163 | + end |
| 164 | +with |
| 165 | + |
| 166 | + var Player p1 with |
| 167 | + this.ren = &renderer!; |
| 168 | + this.x = (640/2 - 20/2) * 1000; |
| 169 | + end; |
| 170 | + var Player p2 with |
| 171 | + this.ren = &renderer!; |
| 172 | + this.x = (640/2 - 20/2) * 1000; |
| 173 | + this.y = (480-50) * 1000; |
| 174 | + end; |
| 175 | + every SDL_DT do |
| 176 | + var int x =0 ; |
| 177 | + end |
| 178 | +with |
| 179 | + every SDL_REDRAW do |
| 180 | + _SDL_RenderPresent(&&renderer!); |
| 181 | + end |
| 182 | +with |
| 183 | + await SDL_QUIT; |
| 184 | +end |
| 185 | + |
| 186 | +escape 0; |
| 187 | + |
0 commit comments