Created
October 9, 2015 11:18
-
-
Save JobLeonard/6044f35a07d91dfc2d9a to your computer and use it in GitHub Desktop.
Drifters, Céu-SDL 0.10 version
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; | |
input _SDL_KeyboardEvent&& SDL_KEYUP; | |
#define PI 3.1415 | |
// proper random double in C: | |
// (double)rand() / ((double)RAND_MAX + 1) * N | |
#define RANDDOUBLE ((f64) _rand() / ( (f64) _RAND_MAX + 1)) | |
var _SDL_Window&? win; | |
finalize | |
win = &_SDL_CreateWindow("SDL 4", _SDL_WINDOWPOS_CENTERED, | |
_SDL_WINDOWPOS_CENTERED, | |
1280, 720, | |
_SDL_WINDOW_SHOWN); | |
with | |
_SDL_DestroyWindow(&&win!); | |
end | |
var _SDL_Renderer&? ren; | |
finalize | |
ren = &_SDL_CreateRenderer(&&win!, -1, 0); | |
with | |
_SDL_DestroyRenderer(&&ren!); | |
end | |
var int ren_w; | |
var int ren_h; | |
_SDL_GetWindowSize(&&win!, &&ren_w, &&ren_h); | |
_SDL_SetRenderDrawBlendMode(&&ren!, _SDL_BLENDMODE_ADD); | |
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 drifter knows her own position, handles her own motion | |
// She wraps around if outside of the screen | |
// Drawing is handled externally, because passing the renderer | |
// was a pain. | |
class Drifter with | |
var f64 x, y, v, t, dt, ddt, feedback; | |
var int w, h; | |
var _SDL_Color clr; | |
do | |
every SDL_DT do | |
// Move | |
// A form of negative feedback is used to determine angular velocity, to give | |
// a smooth looking but still random motion. | |
// TODO: Make a nice little slider GUI that lets me tweak these magic values during runtime | |
ddt = 0.5*ddt + (( ( (f64) _rand() / ( ((f64) _RAND_MAX) + 1)) - 0.5) * 1 - 0.5*dt); | |
dt = (1-feedback)*dt + feedback*ddt; | |
// t is in the range of -1 to 1 | |
t = _fmod(t + 1 + dt + 2, 2) - 1; | |
x = x + _sin(t*PI)*v; | |
y = y + _cos(t*PI)*v; | |
// Wrap around if outside of the screen | |
if x > w then | |
x = x - w; | |
else/if x < 0 then | |
x = x + w; | |
end | |
if y > h then | |
y = y - h; | |
else/if y < 0 then | |
y = y + h; | |
end | |
end | |
end | |
// initialise drifters | |
#define TOTALDRIFTERS 100000 | |
var Drifter[TOTALDRIFTERS] drifters; | |
loop i in TOTALDRIFTERS do | |
var Drifter& d = &drifters[i]; | |
d.w = ren_w; | |
d.h = ren_h; | |
d.x = _rand() % ren_w; | |
d.y = _rand() % ren_h; | |
d.v = RANDDOUBLE * 0.8 + 0.2; // random value between 0.2 and 1.0 | |
d.t = RANDDOUBLE * 2 - 1; // random value between -1.0 and 1.0 | |
d.dt = 0.0; | |
d.ddt = 0.0; | |
d.feedback = RANDDOUBLE * RANDDOUBLE * RANDDOUBLE * 0.01; | |
d.clr.r = _rand() % 256; | |
d.clr.g = _rand() % 256; | |
d.clr.b = _rand() % 256; | |
end | |
par/or do | |
await SDL_QUIT; | |
with | |
// exit if escape key is pressed | |
var _SDL_KeyboardEvent&& key = await SDL_KEYDOWN until key:keysym.sym == _SDLK_ESCAPE; | |
with | |
every SDL_REDRAW do | |
_SDL_SetRenderDrawBlendMode(&&ren!, _SDL_BLENDMODE_BLEND); | |
_SDL_SetRenderDrawColor(&&ren!, 0x0, 0x0, 0x0, 0x05); | |
_SDL_RenderFillRect(&&ren!, &&bg); | |
end | |
with | |
//Have them randomly "eat" each other | |
every SDL_DT do | |
loop i in (TOTALDRIFTERS>>2) do | |
var Drifter& dj = &drifters[_rand()%TOTALDRIFTERS]; | |
var Drifter& dk = &drifters[_rand()%TOTALDRIFTERS]; | |
dj.x = dk.x; | |
dj.y = dk.y; | |
dj.v = ((f64) _rand() / ( (f64) _RAND_MAX + 1))*0.8 + 0.2; | |
dj.t = dk.t; | |
dj.dt = dk.dt; | |
dj.ddt = dk.ddt; | |
dj.feedback = RANDDOUBLE * RANDDOUBLE * RANDDOUBLE * 0.01; | |
// Inject some mutation | |
if _rand() % 256 != 0 then | |
dj.clr = dk.clr; | |
else | |
dj.clr.r = _rand() % 256; | |
dj.clr.g = _rand() % 256; | |
dj.clr.b = _rand() % 256; | |
end | |
end | |
end | |
with | |
every SDL_REDRAW do | |
// Draw drifters | |
loop i in TOTALDRIFTERS do | |
var Drifter& d = &drifters[i]; | |
_pixelRGBA( | |
&&ren!, | |
(int)d.x, | |
(int)d.y, | |
d.clr.r, | |
d.clr.g, | |
d.clr.b, | |
0xFF); | |
end | |
_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