First window rendered, sdl here I come
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
const c = @cImport({
|
||||
@cInclude("SDL2/SDL.h");
|
||||
});
|
||||
|
||||
const std = @import("std");
|
||||
const assert = std.debug.assert;
|
||||
|
||||
pub fn main() !void {
|
||||
if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
|
||||
c.SDL_Log("Unable to initialize SDL: %s", c.SDL_GetError());
|
||||
return error.SDLInitializationFailed;
|
||||
}
|
||||
defer c.SDL_Quit();
|
||||
|
||||
const screen = c.SDL_CreateWindow("My Game Window", c.SDL_WINDOWPOS_UNDEFINED, c.SDL_WINDOWPOS_UNDEFINED, 400, 140, c.SDL_WINDOW_OPENGL) orelse
|
||||
{
|
||||
c.SDL_Log("Unable to create window: %s", c.SDL_GetError());
|
||||
return error.SDLInitializationFailed;
|
||||
};
|
||||
defer c.SDL_DestroyWindow(screen);
|
||||
|
||||
const renderer = c.SDL_CreateRenderer(screen, -1, 0) orelse {
|
||||
c.SDL_Log("Unable to create renderer: %s", c.SDL_GetError());
|
||||
return error.SDLInitializationFailed;
|
||||
};
|
||||
defer c.SDL_DestroyRenderer(renderer);
|
||||
|
||||
const zig_bmp = @embedFile("zig.bmp");
|
||||
const rw = c.SDL_RWFromConstMem(zig_bmp, zig_bmp.len) orelse {
|
||||
c.SDL_Log("Unable to get RWFromConstMem: %s", c.SDL_GetError());
|
||||
return error.SDLInitializationFailed;
|
||||
};
|
||||
defer assert(c.SDL_RWclose(rw) == 0);
|
||||
|
||||
const zig_surface = c.SDL_LoadBMP_RW(rw, 0) orelse {
|
||||
c.SDL_Log("Unable to load bmp: %s", c.SDL_GetError());
|
||||
return error.SDLInitializationFailed;
|
||||
};
|
||||
defer c.SDL_FreeSurface(zig_surface);
|
||||
|
||||
const zig_texture = c.SDL_CreateTextureFromSurface(renderer, zig_surface) orelse {
|
||||
c.SDL_Log("Unable to create texture from surface: %s", c.SDL_GetError());
|
||||
return error.SDLInitializationFailed;
|
||||
};
|
||||
defer c.SDL_DestroyTexture(zig_texture);
|
||||
|
||||
var quit = false;
|
||||
while (!quit) {
|
||||
var event: c.SDL_Event = undefined;
|
||||
while (c.SDL_PollEvent(&event) != 0) {
|
||||
switch (event.type) {
|
||||
c.SDL_QUIT => {
|
||||
quit = true;
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
_ = c.SDL_RenderClear(renderer);
|
||||
_ = c.SDL_RenderCopy(renderer, zig_texture, null, null);
|
||||
c.SDL_RenderPresent(renderer);
|
||||
|
||||
c.SDL_Delay(17);
|
||||
}
|
||||
}
|
||||
|
||||
test "simple test" {
|
||||
var list = std.ArrayList(i32).init(std.testing.allocator);
|
||||
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
|
||||
try list.append(42);
|
||||
try std.testing.expectEqual(@as(i32, 42), list.pop());
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
export fn add(a: i32, b: i32) i32 {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
test "basic add functionality" {
|
||||
try testing.expect(add(3, 7) == 10);
|
||||
}
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 164 KiB |
Reference in New Issue
Block a user