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"] [submodule "lib/SDL.zig"]
path = lib/SDL.zig path = lib/SDL.zig
url = ../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); const sdk = Sdk.init(b);
sdk.link(exe, .dynamic); 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.setTarget(target);
exe.setBuildMode(mode); exe.setBuildMode(mode);

1
lib/zgl Submodule

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

View File

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