From ccfeb1fe1b4bf46b1b022dc6a5b74f2455b68368 Mon Sep 17 00:00:00 2001 From: Dendy Date: Thu, 21 Jul 2022 17:55:22 +0200 Subject: [PATCH] WIP: open an image and clear with OpenGL --- .gitmodules | 3 ++ build.zig | 6 +++- lib/zgl | 1 + src/main.zig | 82 ++++++++++++++++++++++++++++------------------------ 4 files changed, 54 insertions(+), 38 deletions(-) create mode 160000 lib/zgl diff --git a/.gitmodules b/.gitmodules index 92af0ff..ec33071 100644 --- a/.gitmodules +++ b/.gitmodules @@ -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 diff --git a/build.zig b/build.zig index 5bfd975..d774945 100644 --- a/build.zig +++ b/build.zig @@ -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); diff --git a/lib/zgl b/lib/zgl new file mode 160000 index 0000000..32608da --- /dev/null +++ b/lib/zgl @@ -0,0 +1 @@ +Subproject commit 32608da70ca057ec09438dbf6549834dcfbeb52c diff --git a/src/main.zig b/src/main.zig index a9f0372..fd8cc79 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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; + while (sdl.pollEvent()) |ev| { + switch (ev) { + .quit => break :mainLoop, + else => {}, + } } - _ = SDL.SDL_SetRenderDrawColor(renderer, 231, 247, 255, 0xFF); - _ = SDL.SDL_RenderClear(renderer); + gl.clear(.{}); - current_state = switch (current_state) { - .main_menu => main_menu.tick(), - .game => game.tick(), - }; + sdl.gl.swapWindow(window); - SDL.SDL_RenderPresent(renderer); + //_ = 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 { + +}