Last active
December 21, 2015 03:39
-
-
Save pszturmaj/6244260 to your computer and use it in GitHub Desktop.
LDC2 + Emscripten + SDL + std.algorithms's sort & map
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
// this is modified example | |
// original is from: http://d.hatena.ne.jp/ABA/20130331#p1 | |
import SDL; | |
import SDL_video; | |
import std.stdio; | |
import std.algorithm; | |
immutable width = 640, height = 480; | |
SDL_Surface *screen; | |
int ticks = 0; | |
void update() { | |
SDL_LockSurface(screen); | |
for (int i = 0; i < height; i++) { | |
for (int j = 0; j < width; j++) { | |
*(cast(char*)screen.pixels + i*width*4 + j*4 + 0) = cast(char)(j + ticks) % 128; | |
*(cast(char*)screen.pixels + i*width*4 + j*4 + 1) = cast(char)(i + ticks) % 128; | |
*(cast(char*)screen.pixels + i*width*4 + j*4 + 2) = cast(char)((255-i) + ticks) % 128; | |
} | |
} | |
SDL_UnlockSurface(screen); | |
SDL_Flip(screen); | |
ticks++; | |
} | |
extern(C): | |
void mainLoop() { | |
update(); | |
} | |
void emscripten_set_main_loop(void function(), int fps, int simulate_infinite_loop); | |
int main() { | |
auto arr = [10, 11, 12]; // this needed some additions to emscripten's library.js (_d_newarrayT/_d_newarrayvT) | |
auto sr = sort(arr).map!(a => a * a)(); | |
foreach (i; sr) | |
printf("i: %d\n", i); // writeln() and friends are currently broken | |
SDL_Init(SDL_INIT_VIDEO); | |
screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE); | |
emscripten_set_main_loop(&mainLoop, 60, 1); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment