-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.zig
More file actions
154 lines (114 loc) · 5.12 KB
/
main.zig
File metadata and controls
154 lines (114 loc) · 5.12 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const std = @import("std");
const builtin = @import("builtin");
const c = @import("CImports.zig").c;
const Texture = @import("Easy2D/Texture.zig").Texture;
const Graphics = @import("Easy2D/Graphics.zig").Graphics;
const DrawableManager = @import("Drawables/DrawableManager.zig").DrawableManager;
const DrawableData = @import("Drawables/DrawableManager.zig").DrawableData;
const Player = @import("Drawables/Player.zig").Player;
const SceneManager = @import("Scenes/SceneManager.zig").SceneManager;
const MenuScene = @import("Scenes/MenuScene.zig").MenuScene;
const PlayScene = @import("Scenes/PlayScene.zig").PlayScene;
const TestScene = @import("Scenes/TestScene.zig").TestScene;
const Viewport = @import("Easy2D/Viewport.zig").Viewport;
const zm = @import("zm");
const Sound = @import("Sound.zig").Sound;
const Beatmap = @import("Osu/OsuParser.zig").Beatmap;
const PlayableBeatmap = @import("Osu/PlayableBeatmap.zig").PlayableBeatmap;
const Replay = @import("Osu/ReplayParser.zig").Replay;
pub fn main() !void {
std.debug.print("\nHello zig!\n\n", .{});
if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
std.debug.print("SDL_Init Error: {s}\n", .{c.SDL_GetError()});
return;
}
defer c.SDL_Quit();
// Request OpenGL ES 2.0 context
_ = c.SDL_GL_SetAttribute(c.SDL_GL_CONTEXT_PROFILE_MASK, c.SDL_GL_CONTEXT_PROFILE_ES);
_ = c.SDL_GL_SetAttribute(c.SDL_GL_CONTEXT_MAJOR_VERSION, 2);
_ = c.SDL_GL_SetAttribute(c.SDL_GL_CONTEXT_MINOR_VERSION, 0);
var width: i32 = 1280;
var height: i32 = 720;
const window = c.SDL_CreateWindow("game", c.SDL_WINDOWPOS_CENTERED, c.SDL_WINDOWPOS_CENTERED, width, height, c.SDL_WINDOW_OPENGL | c.SDL_WINDOW_RESIZABLE);
if (window == null) {
std.debug.print("SDL_CreateWindow Error: {s}\n", .{c.SDL_GetError()});
return;
}
defer c.SDL_DestroyWindow(window);
Sound.InitBass(window);
const gl_context = c.SDL_GL_CreateContext(window);
if (gl_context == null) {
std.debug.print("SDL_GL_CreateContext Error: {s}\n", .{c.SDL_GetError()});
return;
}
defer c.SDL_GL_DeleteContext(gl_context);
_ = c.SDL_GL_SetSwapInterval(0);
c.glDisable(c.GL_DEPTH_TEST);
c.glEnable(c.GL_TEXTURE);
c.glEnable(c.GL_BLEND);
c.glBlendFunc(c.GL_SRC_ALPHA, c.GL_ONE_MINUS_SRC_ALPHA);
//c.glClearColor(0.39, 0.58, 0.93, 1.0);
c.glClearColor(0.0, 0.0, 0.0, 1.0);
var g = try Graphics.Init();
var event: c.SDL_Event = undefined;
var running = true;
var mousePos: zm.Vec2f = .{ 0.0, 0.0 };
var prev = try std.time.Instant.now();
var total_time: f32 = 0.0;
var fps: i32 = 0;
var fpsTimer: f64 = 0.0;
onResize(&g, width, height);
SceneManager.GetInstance().AddScene(PlayScene, PlayScene.GetInstance(), PlayScene.GetFnTable());
SceneManager.GetInstance().AddScene(MenuScene, MenuScene.GetInstance(), MenuScene.GetFnTable());
SceneManager.GetInstance().AddScene(TestScene, TestScene.GetInstance(), TestScene.GetFnTable());
while (running) {
const now = try std.time.Instant.now();
const delta_ns = now.since(prev); //nanoseconds
prev = now;
const d_delta = @as(f64, @floatFromInt(delta_ns)) / 1_000_000_000.0;
const delta: f32 = @floatCast(d_delta);
total_time += delta;
fps += 1;
fpsTimer += d_delta;
if (fpsTimer >= 1.0) {
std.debug.print("FPS: {d}\n", .{fps});
fps = 0;
fpsTimer -= 1.0;
}
while (c.SDL_PollEvent(&event) != 0) {
if (event.type == c.SDL_QUIT) {
running = false;
}
if (event.type == c.SDL_MOUSEMOTION) {
mousePos = .{ @floatFromInt(event.motion.x), @floatFromInt(event.motion.y) };
//std.debug.print("Mouse X: {d} Y: {d}\n", .{ mouseX, mouseY });
}
if (event.type == c.SDL_WINDOWEVENT) {
if (event.window.event == c.SDL_WINDOWEVENT_SIZE_CHANGED) {
width = event.window.data1;
height = event.window.data2;
//std.debug.print("Resized: {d},{d}\n", .{ width, height });
onResize(&g, width, height);
}
}
SceneManager.OnEvent(&event);
}
//clear screen to cornflower blue
c.glClear(c.GL_COLOR_BUFFER_BIT);
g.Time = total_time;
SceneManager.OnUpdate(delta);
SceneManager.OnDraw(&g);
//g.DrawRectangleCentered(mousePos, .{ 16.0, 16.0 }, .{ 1.0, 0.0, 0.0, 1.0 }, &Play, .{ 0.0, 0.0, 1.0, 1.0 });
g.EndDraw();
c.SDL_GL_SwapWindow(window);
//return;
//A little bit of sleep so i dont burn my lap while debugging.
if (builtin.mode == .Debug)
std.Thread.sleep(1_000_000);
}
}
fn onResize(g: *Graphics, window_width: i32, window_height: i32) void {
g.ProjectionMatrix = zm.Mat4f.orthographic(0.0, @floatFromInt(window_width), @floatFromInt(window_height), 0.0, -1.0, 1.0);
PlayableBeatmap.UpdatePlayfield(@floatFromInt(window_width), @floatFromInt(window_height));
Viewport.SetViewport(0, 0, window_width, window_height);
}