Skip to content

Commit

Permalink
feat(examples): Game of life + SDL
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Sep 9, 2023
1 parent f4acc95 commit b5aff56
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 8 deletions.
98 changes: 91 additions & 7 deletions examples/game-of-life.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import "std";
import "io" as io;
import "os";
import "debug";
| import "examples/sdl-wrapped";
import "examples/sdl-wrapped";

| buzz -L/path/to/SDL2.dylib/so/dll examples/game-of-life.buzz
object World {
int width,
int height,
Expand Down Expand Up @@ -63,20 +64,103 @@ object World {
io.stdout.write("\n") catch void;
}
}

fun draw(Renderer renderer) > void !> SDLError {
const int cellWidth = 800 / this.width;
const int cellHeight = 600 / this.height;

for (int y = 0; y < this.height; y = y + 1) {
for (int x = 0; x < this.width; x = x + 1) {
renderer.setRenderDrawColor(
if (this.cells[y * this.width + x])
Color{
red = 0x01,
green = 0x9d,
blue = 0xe0,
}
else
Color{
red = 0xff,
green = 0xff,
blue = 0xff,
}
);

renderer.fillRect(
Rect{
x = cellWidth * x,
y = cellHeight * y,
width = cellWidth,
height = cellHeight,
}
);
}
}

renderer.setRenderTarget(null);
renderer.setRenderDrawColor(
Color{
red = 0,
green = 0,
blue = 0,
}
);
}
}

fun main([str] args) > void {
fun main([str] args) > void !> SDLError {
SDL sdl = SDL.init([Subsystem.Video]);

Window window = Window.init(
"buzz example: Game of Life",
width: 800,
height: 600,
flags: [WindowFlag.OpenGL, WindowFlag.AllowHighDPI],
);

Renderer renderer = window.createRenderer(
index: -1,
flags: [RendererFlag.Accelerated, RendererFlag.TargetTexture],
);

Texture texture = renderer.createTexture(
format: PixelFormat.RGBA8888,
access: TextureAccess.Target,
width: 800,
height: 600,
);

int? width = if (args.len() > 0) parseInt(args[0]) else null;
int? height = if (args.len() > 1) parseInt(args[1]) else null;

World world = World.init(width: width ?? 10, height: height ?? 10);
World world = World.init(
width: width ?? 10,
height: height ?? 10,
);

float dt = sdl.ticks();
while (true) {
io.stdout.write("\027[2J") catch void;
if (sdl.pollEvent() -> event) {
if (event.@"type" == EventType.Quit.value) {
break;
}
}

world.dump();
world.step();
if (sdl.ticks() - dt > 300) {
dt = sdl.ticks();
renderer.setRenderTarget(texture);
world.draw(renderer);
renderer.setRenderTarget(null);

sleep(500.0);
world.step();
}

renderer.renderCopy(texture);

renderer.renderPresent();

sdl.delay(100.0);
}

sdl.quit();
}
5 changes: 5 additions & 0 deletions examples/sdl-wrapped.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ zdef("SDL2", `
fn SDL_DestroyTexture(texture: *SDL_Texture) void;
fn SDL_SetRenderTarget(renderer: *SDL_Renderer, texture: ?*SDL_Texture) c_int;
fn SDL_RenderCopy(renderer: *SDL_Renderer, texture: *SDL_Texture, srcrect: ?*const SDL_Rect, dstrect: ?*const SDL_Rect) c_int;
fn SDL_GetTicks() u32;
`);

export enum(int) EventType {
Expand Down Expand Up @@ -253,6 +254,10 @@ export object SDL {
fun delay(float ms) > void {
SDL_Delay(ms);
}

fun ticks() > float {
return SDL_GetTicks();
}
}

export object Texture {
Expand Down
2 changes: 1 addition & 1 deletion examples/sdl.buzz
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "std";
import "examples/sdl-wrapped";

| buzz -L/path/to/SDL2.dylib/so/dll sdl.buzz
| buzz -L/path/to/SDL2.dylib/so/dll examples/sdl.buzz
fun main([str] args) > int !> SDLError {
SDL sdl = SDL.init([Subsystem.Video]);

Expand Down

0 comments on commit b5aff56

Please sign in to comment.