Skip to content

Commit

Permalink
Rework ViewLocator to fix init bug
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlangston committed Dec 12, 2023
1 parent 63d5731 commit ca502ba
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 56 deletions.
6 changes: 2 additions & 4 deletions src/Shared.zig
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,8 @@ pub const Shared = struct {
};

pub inline fn init() !void {
for (std.enums.values(Shared.View.Views)) |view| {
var v = Shared.View.ViewLocator.Build(view);
v.init();
}
const menu = Shared.View.ViewLocator.Build(.Menu);
menu.init();

raylib.setConfigFlags(
@enumFromInt( //@intFromEnum(raylib.ConfigFlags.flag_window_always_run) +
Expand Down
42 changes: 17 additions & 25 deletions src/ViewLocator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,25 @@ const BaseViewModel = @import("./ViewModels/ViewModel.zig").ViewModel;
const BaseView = @import("./Views/View.zig").View;

pub const ViewLocator = struct {
const AllViews = [_]BaseView{
@import("./Views/RaylibSplashScreenView.zig").RaylibSplashScreenView,
@import("./Views/DylanSplashScreenView.zig").DylanSplashScreenView,
@import("./Views/PausedView.zig").PausedView,
@import("./Views/AsteroidsView.zig").AsteroidsView,
@import("./Views/MenuView.zig").MenuView,
@import("./Views/SettingsView.zig").SettingsView,
@import("./Views/GameOverView.zig").GameOverView,
};

inline fn GetView(view: Views) BaseView {
switch (view) {
Views.Raylib_Splash_Screen => {
return @import("./Views/RaylibSplashScreenView.zig").RaylibSplashScreenView;
},
Views.Dylan_Splash_Screen => {
return @import("./Views/DylanSplashScreenView.zig").DylanSplashScreenView;
},
Views.Paused => {
return @import("./Views/PausedView.zig").PausedView;
},
Views.Asteroids => {
return @import("./Views/AsteroidsView.zig").AsteroidsView;
},
Views.Menu => {
return @import("./Views/MenuView.zig").MenuView;
},
Views.Settings => {
return @import("./Views/SettingsView.zig").SettingsView;
},
Views.Game_Over => {
return @import("./Views/GameOverView.zig").GameOverView;
},
else => {
return BaseView{ .DrawRoutine = DrawQuit };
},
inline for (AllViews) |v| {
if (v.Key == view) return v;
}

return BaseView{
.Key = .Quit,
.DrawRoutine = DrawQuit,
};
}

fn DrawQuit() Views {
Expand Down
2 changes: 0 additions & 2 deletions src/ViewModels/AsteroidsViewModel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
};
}

Shared.Log.Info("About to call random...");

// Initialization Big Meteor
for (0..MAX_BIG_METEORS) |i| {
posx = Shared.Random.Get().float(f32) * screenSize.x;
Expand Down
12 changes: 1 addition & 11 deletions src/ViewModels/MenuViewModel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub const MenuViewModel = Shared.View.ViewModel.Create(
struct {
pub var selection = Selection.Start;
pub var Rectangles: [std.enums.directEnumArrayLen(Selection, 0) - 1]raylib.Rectangle = undefined;
pub var offset_y: f32 = 0;

pub inline fn GetSelectionText(select: Selection) [:0]const u8 {
const locale = Shared.Locale.GetLocale().?;
Expand All @@ -35,14 +34,5 @@ pub const MenuViewModel = Shared.View.ViewModel.Create(
}
}
},
.{
.Init = init,
.DeInit = deinit,
},
.{},
);

fn init() void {
MenuViewModel.GetVM().offset_y = 0;
}

fn deinit() void {}
1 change: 1 addition & 0 deletions src/Views/AsteroidsView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ fn DrawWithCamera() Shared.View.Views {
}

pub const AsteroidsView = Shared.View.View{
.Key = .Asteroids,
.DrawRoutine = &DrawWithCamera,
.VM = &AsteroidsViewModel,
};
3 changes: 2 additions & 1 deletion src/Views/DylanSplashScreenView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn DrawFunction() Shared.View.Views {
raylib.clearBackground(Shared.Color.Tone.Dark);

const text = Shared.Locale.GetLocale().?.Dylan_Splash_Text;
const font = Shared.Font.Get(.Unknown);
const font = Shared.Font.Get(.TwoLines);
const screenWidth: f32 = @floatFromInt(raylib.getScreenWidth());
const screenHeight: f32 = @floatFromInt(raylib.getScreenHeight());
const fontSize: f32 = screenWidth / 25;
Expand Down Expand Up @@ -86,6 +86,7 @@ fn DrawFunction() Shared.View.Views {
}

pub const DylanSplashScreenView = Shared.View.View{
.Key = .Dylan_Splash_Screen,
.DrawRoutine = DrawFunction,
.VM = &DylanSplashScreenViewModel,
};
1 change: 1 addition & 0 deletions src/Views/GameOverView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub fn DrawFunction() Shared.View.Views {
}

pub const GameOverView = Shared.View.View{
.Key = .Game_Over,
.DrawRoutine = &DrawFunction,
.VM = &GameOverViewModel,
};
8 changes: 1 addition & 7 deletions src/Views/MenuView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,9 @@ pub fn DrawFunction() Shared.View.Views {
const title = locale.Title;
const screenWidth = raylib.getScreenWidth();
const screenHeight = raylib.getScreenHeight();
const screenHeightF: f32 = @floatFromInt(screenHeight);
const fontSize = @divFloor(screenWidth, 20);
const startY = @divFloor(screenHeight, 4);

const scroll_speed: f32 = 20 * raylib.getFrameTime();
vm.offset_y += scroll_speed;
if (vm.offset_y > screenHeightF) {
vm.offset_y -= screenHeightF;
}

const foregroundColor = Shared.Color.Blue.Base;
const backgroundColor = Shared.Color.Blue.Light.alpha(0.75);
const accentColor = Shared.Color.Blue.Dark;
Expand Down Expand Up @@ -157,6 +150,7 @@ inline fn GetSelection() Shared.View.Views {
}

pub const MenuView = Shared.View.View{
.Key = .Menu,
.DrawRoutine = DrawFunction,
.VM = &MenuViewModel,
};
1 change: 1 addition & 0 deletions src/Views/PausedView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ inline fn GetSelection() Shared.View.Views {
}

pub const PausedView = Shared.View.View{
.Key = .Paused,
.DrawRoutine = &DrawFunction,
.VM = &PausedViewModel,
};
1 change: 1 addition & 0 deletions src/Views/RaylibSplashScreenView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ pub const States = enum {
};

pub const RaylibSplashScreenView = View{
.Key = .Raylib_Splash_Screen,
.DrawRoutine = DrawFunction,
.VM = &SplashScreenViewModel,
};
1 change: 1 addition & 0 deletions src/Views/SettingsView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ pub fn DrawFunction() Shared.View.Views {
}

pub const SettingsView = Shared.View.View{
.Key = .Settings,
.DrawRoutine = &DrawFunction,
};
15 changes: 9 additions & 6 deletions src/Views/View.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ const Views = @import("../ViewLocator.zig").Views;
const Shared = @import("../Shared.zig").Shared;

pub const View = struct {
Key: Views,
DrawRoutine: *const fn () Views,
VM: *const ViewModel = undefined,

// Initialize View Model if needed
var isInitialized = false;
var initializedViews: std.EnumSet(Views) = std.EnumSet(Views).initEmpty();

pub inline fn init(self: View) void {
if (isInitialized == false and @intFromPtr(self.VM) != 0 and @intFromPtr(self.VM.*.Init) != 0) {
if (!initializedViews.contains(self.Key) and @intFromPtr(self.VM) != 0 and @intFromPtr(self.VM.*.Init) != 0) {
self.VM.*.Init();
isInitialized = true;
initializedViews.insert(self.Key);
}
}
pub inline fn deinit(self: View) void {
if (@intFromPtr(self.VM) != 0 and @intFromPtr(self.VM.*.DeInit) != 0 and (isInitialized == true or @intFromPtr(self.VM.*.Init) == 0)) {
if (@intFromPtr(self.VM) != 0 and @intFromPtr(self.VM.*.DeInit) != 0) {
self.VM.*.DeInit();
isInitialized = false;
}
if (initializedViews.contains(self.Key) == true) {
initializedViews.remove(self.Key);
}
}
pub inline fn shouldBypassDeinit(self: View) bool {
Expand Down

0 comments on commit ca502ba

Please sign in to comment.