Last active
August 29, 2015 14:11
-
-
Save JobLeonard/254c273e419ad1c5c568 to your computer and use it in GitHub Desktop.
First attempt at porting DwFE to Ceu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "sdl.ceu" | |
#include "sdl-gfx.ceu" | |
input void SDL_REDRAW; | |
input void SDL_QUIT; | |
input int SDL_DT; | |
input _SDL_KeyboardEvent* SDL_KEYDOWN; | |
native do | |
SDL_Renderer* REN = NULL; | |
int REN_W, REN_H; | |
end | |
var _SDL_Window[] win; | |
finalize | |
win = _SDL_CreateWindow("Drifters", _SDL_WINDOWPOS_CENTERED, | |
_SDL_WINDOWPOS_CENTERED, | |
800, 600, | |
_SDL_WINDOW_SHOWN); | |
with | |
_SDL_DestroyWindow(win); | |
end | |
_SDL_GetWindowSize(win, &_REN_W, &_REN_H); | |
finalize | |
_REN = _SDL_CreateRenderer(win, -1, 0); | |
with | |
_SDL_DestroyRenderer(_REN); | |
end | |
var _SDL_Rect bg; | |
bg.w = _REN_W; | |
bg.h = _REN_H; | |
bg.x = 0; | |
bg.y = 0; | |
var _SDL_Color bg_clr; | |
bg_clr.r = 0x00; | |
bg_clr.g = 0x00; | |
bg_clr.b = 0x00; | |
var _SDL_Color white; | |
white.r = 0xFF; | |
white.g = 0xFF; | |
white.b = 0xFF; | |
// A dancer knows her own position and draws herself, and wraps around if outside of the screen | |
class Dancer with | |
var f64 x, y; | |
var _SDL_Color clr; | |
do | |
every SDL_REDRAW do | |
if this.x > _REN_W then | |
this.x = this.x - _REN_W; | |
else/if this.x < 0 then | |
this.x = this.x + _REN_W; | |
end | |
if this.y > _REN_H then | |
this.y = this.y - _REN_H; | |
else/if this.y < 0 then | |
this.y = this.y + _REN_H; | |
end | |
_pixelRGBA( | |
_REN, | |
(int)x, | |
(int)y, | |
this.clr.r, | |
this.clr.g, | |
this.clr.b, | |
0xFF); | |
end | |
end | |
// If I crank it up beyond 80000 I get a segfault | |
#define TOTALDANCERS 80000 | |
par/or do | |
await SDL_QUIT; | |
with | |
loop do | |
var _SDL_KeyboardEvent* key = await SDL_KEYDOWN; | |
if key:keysym.sym == _SDLK_ESCAPE then | |
// exit program | |
break; | |
end | |
end | |
with | |
// clear background | |
every SDL_REDRAW do | |
_SDL_SetRenderDrawColor(_REN, bg_clr.r,bg_clr.g,bg_clr.b,0xFF); | |
_SDL_RenderFillRect(_REN, &bg); | |
end | |
with | |
loop do | |
par/or do | |
// Initialise dancers, vectors for their displacement, and vectors | |
// storing the indices of friends and enemies | |
// TODO: Split this into something like a Choreography class | |
var Dancer[TOTALDANCERS] dancers; | |
var f64[TOTALDANCERS] deltax, deltay; | |
var int[TOTALDANCERS] friends, enemies; | |
// Initialise variables | |
loop i in TOTALDANCERS do | |
dancers[i].x = (_rand() % (_REN_W/2)) + _REN_W/4; | |
dancers[i].y = (_rand() % (_REN_H/2)) + _REN_H/4; | |
dancers[i].clr = white; | |
deltax[i] = 0; | |
deltay[i] = 0; | |
// make sure the indices for friend/enemy are other dancers | |
friends[i] = (i + 1 + ( _rand() % (TOTALDANCERS - 1))) % TOTALDANCERS; | |
enemies[i] = (i + 1 + ( _rand() % (TOTALDANCERS - 1))) % TOTALDANCERS; | |
end; | |
// Run the simulation | |
var f64 cx = _REN_W * 0.5; | |
var f64 cy = _REN_H * 0.5; | |
var int dt; | |
every dt in SDL_DT do | |
loop i in TOTALDANCERS do | |
// 1 in 10000 chance of changing friends/enemies | |
if (_rand()%10000) == 0 then | |
friends[i] = (i + 1 + ( _rand() % (TOTALDANCERS - 1))) % TOTALDANCERS; | |
enemies[i] = (i + 1 + ( _rand() % (TOTALDANCERS - 1))) % TOTALDANCERS; | |
end | |
var int fr = friends[i]; | |
var int en = enemies[i]; | |
var f64 x0 = dancers[i].x; | |
var f64 y0 = dancers[i].y; | |
// calculate normalised vector towards friend | |
var f64 frdx = dancers[fr].x - x0; | |
var f64 frdy = dancers[fr].y - y0; | |
var f64 dsq = (frdx*frdx + frdy*frdy); | |
if dsq > 0 then | |
dsq = _sqrt(dsq); | |
frdx = frdx / dsq; | |
frdy = frdy / dsq; | |
else | |
frdx = 0; | |
frdy = 0; | |
end | |
// calculate normalised vector towards enemy | |
var f64 endx = dancers[en].x - x0; | |
var f64 endy = dancers[en].y - y0; | |
dsq = (endx*endx + endy*endy); | |
if dsq > 0 then | |
dsq = _sqrt(dsq); | |
endx = endx / dsq; | |
endy = endy / dsq; | |
else | |
endx = 0; | |
endy = 0; | |
end | |
// calculate distance to center | |
var f64 cdx = cx - x0; | |
var f64 cdy = cy - y0; | |
// calculate acceleration towards friend | |
// and acceleration away from enemy | |
// and acceleration towards the center | |
deltax[i] = deltax[i] * 0.9 + (frdx * 0.06 - endx * 0.07 + 0.0002*cdx) * 0.1 * dt; | |
deltay[i] = deltay[i] * 0.9 + (frdy * 0.06 - endy * 0.07 + 0.0002*cdy) * 0.1 * dt; | |
end | |
// apply steps | |
loop i in TOTALDANCERS do | |
dancers[i].x = dancers[i].x + deltax[i]; | |
dancers[i].y = dancers[i].y + deltay[i]; | |
end | |
end; | |
with | |
loop do | |
var _SDL_KeyboardEvent* key = await SDL_KEYDOWN; | |
if key:keysym.sym != _SDLK_ESCAPE then | |
// Reset the simulation | |
break; | |
end | |
end | |
end | |
end | |
with | |
every SDL_REDRAW do | |
_SDL_RenderPresent(_REN); | |
end | |
end | |
escape 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment