WIP: open an image and clear with OpenGL

This commit is contained in:
Dendy 2022-07-21 17:55:22 +02:00
parent 65c1f5eb34
commit ccfeb1fe1b
4 changed files with 54 additions and 38 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "lib/SDL.zig"]
path = lib/SDL.zig
url = ../SDL.zig
[submodule "lib/zgl"]
path = lib/zgl
url = https://github.com/ziglibs/zgl

View File

@ -17,7 +17,11 @@ pub fn build(b: *std.build.Builder) void {
const sdk = Sdk.init(b);
sdk.link(exe, .dynamic);
exe.addPackage(sdk.getNativePackage("sdl2"));
exe.addPackagePath("zgl", "lib/zgl/zgl.zig");
exe.addPackage(sdk.getWrapperPackage("sdl2"));
exe.linkSystemLibrary("gl");
exe.linkSystemLibrary("epoxy");
exe.setTarget(target);
exe.setBuildMode(mode);

1
lib/zgl Submodule

@ -0,0 +1 @@
Subproject commit 32608da70ca057ec09438dbf6549834dcfbeb52c

View File

@ -1,59 +1,67 @@
const std = @import("std");
const SDL = @import("sdl2");
const sdl = @import("sdl2");
const gl = @import("zgl");
const Game = @import("Game.zig");
const MainMenu = @import("MainMenu.zig");
//const Game = @import("Game.zig");
//const MainMenu = @import("MainMenu.zig");
const State = @import("flow.zig").State;
//const State = @import("flow.zig").State;
pub fn main() !void {
const stderr = std.io.getStdErr();
defer stderr.close();
try sdl.init(.{ .video = true, .audio = true, .events = true });
defer sdl.quit();
// var gpa = std.heap.GeneralPurposeAllocator(.{}){};
// defer _ = gpa.deinit();
// const allocator = gpa.allocator();
try sdl.gl.setAttribute(.{ .context_major_version = 2 });
try sdl.gl.setAttribute(.{ .context_minor_version = 1 });
_ = SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO);
defer SDL.SDL_Quit();
var window = SDL.SDL_CreateWindow(
const window = try sdl.createWindow(
"USG",
SDL.SDL_WINDOWPOS_CENTERED,
SDL.SDL_WINDOWPOS_CENTERED,
.{ .centered = {} },
.{ .centered = {} },
1280,
720,
SDL.SDL_WINDOW_SHOWN,
) orelse return;
defer _ = SDL.SDL_DestroyWindow(window);
.{ .opengl = true, .shown = true }
);
defer window.destroy();
var renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_PRESENTVSYNC | SDL.SDL_RENDERER_ACCELERATED) orelse return;
defer _ = SDL.SDL_DestroyRenderer(renderer);
const ctx = try sdl.gl.createContext(window);
defer sdl.gl.deleteContext(ctx);
var main_menu = MainMenu.init();
var game = Game.init(renderer);
//var renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_PRESENTVSYNC | SDL.SDL_RENDERER_ACCELERATED) orelse return;
//defer _ = SDL.SDL_DestroyRenderer(renderer);
var current_state: State = main_menu.state;
//var main_menu = MainMenu.init();
//var game = Game.init(renderer);
_ = SDL.SDL_SetRenderDrawBlendMode(renderer, SDL.SDL_BLENDMODE_BLEND);
_ = SDL.IMG_Init(SDL.IMG_INIT_JPG);
//var current_state: State = main_menu.state;
//_ = SDL.SDL_SetRenderDrawBlendMode(renderer, SDL.SDL_BLENDMODE_BLEND);
//_ = SDL.IMG_Init(SDL.IMG_INIT_JPG);
mainLoop: while (true) {
var ev: SDL.SDL_Event = undefined;
while (SDL.SDL_PollEvent(&ev) != 0) {
if (ev.type == SDL.SDL_QUIT)
break :mainLoop;
}
_ = SDL.SDL_SetRenderDrawColor(renderer, 231, 247, 255, 0xFF);
_ = SDL.SDL_RenderClear(renderer);
current_state = switch (current_state) {
.main_menu => main_menu.tick(),
.game => game.tick(),
};
SDL.SDL_RenderPresent(renderer);
while (sdl.pollEvent()) |ev| {
switch (ev) {
.quit => break :mainLoop,
else => {},
}
}
gl.clear(.{});
sdl.gl.swapWindow(window);
//_ = SDL.SDL_SetRenderDrawColor(renderer, 231, 247, 255, 0xFF);
//_ = SDL.SDL_RenderClear(renderer);
//current_state = switch (current_state) {
//.main_menu => main_menu.tick(),
//.game => game.tick(),
//};
//SDL.SDL_RenderPresent(renderer);
}
}
fn initGL() void {
}