-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsdl-zig-demo.zig
More file actions
124 lines (109 loc) · 3.89 KB
/
sdl-zig-demo.zig
File metadata and controls
124 lines (109 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const std = @import("std");
const builtin = @import("builtin");
const android = @import("android");
const sdl = @import("sdl");
const log = std.log;
const assert = std.debug.assert;
// custom standard options for Android
pub const std_options: std.Options = if (builtin.abi.isAndroid())
.{
.logFn = android.logFn,
}
else
.{};
// Custom panic handler for Android
pub const panic = if (builtin.abi.isAndroid())
android.panic
else
std.debug.FullPanic(std.debug.defaultPanic);
comptime {
if (builtin.abi.isAndroid()) {
@export(&SDL_main, .{ .name = "SDL_main", .linkage = .strong });
}
}
/// This needs to be exported for Android builds
fn SDL_main() callconv(.c) void {
if (!comptime builtin.abi.isAndroid()) {
@compileError("SDL_main should not be called outside of Android builds");
}
if (builtin.zig_version.major == 0 and builtin.zig_version.minor <= 14) {
_ = std.start.callMain();
return;
}
main() catch |err| {
log.err("{s}", .{@errorName(err)});
if (@errorReturnTrace()) |trace| {
if (builtin.zig_version.major == 0 and builtin.zig_version.minor <= 15) {
std.debug.dumpStackTrace(trace.*);
} else {
std.debug.dumpStackTrace(trace);
}
}
};
}
pub fn main() !void {
log.debug("started sdl-zig-demo", .{});
if (sdl.SDL_Init(sdl.SDL_INIT_VIDEO) < 0) {
log.info("Unable to initialize SDL: {s}", .{sdl.SDL_GetError()});
return error.SDLInitializationFailed;
}
defer sdl.SDL_Quit();
const screen = sdl.SDL_CreateWindow("My Game Window", sdl.SDL_WINDOWPOS_UNDEFINED, sdl.SDL_WINDOWPOS_UNDEFINED, 400, 140, sdl.SDL_WINDOW_OPENGL) orelse {
log.info("Unable to create window: {s}", .{sdl.SDL_GetError()});
return error.SDLInitializationFailed;
};
defer sdl.SDL_DestroyWindow(screen);
const renderer = sdl.SDL_CreateRenderer(screen, -1, 0) orelse {
log.info("Unable to create renderer: {s}", .{sdl.SDL_GetError()});
return error.SDLInitializationFailed;
};
defer sdl.SDL_DestroyRenderer(renderer);
const rw = sdl.SDL_RWFromFile(if (builtin.abi.isAndroid())
// For Android, we setup the build to dynamically add "zig.bmp" to the Android assets folder
// so it's accessible without "src/" prefix
"zig.bmp"
else
"src/zig.bmp", "rb");
defer assert(sdl.SDL_RWclose(rw) == 0);
const zig_surface = sdl.SDL_LoadBMP_RW(rw, 0) orelse {
log.info("Unable to load bmp: {s}", .{sdl.SDL_GetError()});
return error.SDLInitializationFailed;
};
defer sdl.SDL_FreeSurface(zig_surface);
const zig_texture = sdl.SDL_CreateTextureFromSurface(renderer, zig_surface) orelse {
log.info("Unable to create texture from surface: {s}", .{sdl.SDL_GetError()});
return error.SDLInitializationFailed;
};
defer sdl.SDL_DestroyTexture(zig_texture);
var quit = false;
var has_run_frame: FrameLog = .none;
while (!quit) {
if (has_run_frame == .one_frame_passed) {
// NOTE(jae): 2024-10-03
// Allow inspection of logs to see if a frame executed at least once
log.debug("has executed one frame", .{});
has_run_frame = .logged_one_frame;
}
var event: sdl.SDL_Event = undefined;
while (sdl.SDL_PollEvent(&event) != 0) {
switch (event.type) {
sdl.SDL_QUIT => {
quit = true;
},
else => {},
}
}
_ = sdl.SDL_RenderClear(renderer);
_ = sdl.SDL_RenderCopy(renderer, zig_texture, null, null);
sdl.SDL_RenderPresent(renderer);
sdl.SDL_Delay(17);
if (has_run_frame == .none) {
has_run_frame = .one_frame_passed;
}
}
}
const FrameLog = enum {
none,
one_frame_passed,
logged_one_frame,
};