odin-space-invaders/main.odin

125 lines
2.5 KiB
Odin

package space_invaders
import "core:c"
import "core:log"
import glm "core:math/linalg/glsl"
import rl "vendor:raylib"
TEXTURE_ATLAS_PATH :: "./assets/texture_atlas.png"
DEBUG_MODE :: false
GameEndType :: enum {
AllAliensKilled,
PlayerDied,
AliensReachedPlayer,
}
state: GameState
ratio: f32
texture_atlas_image: rl.Image
texture_atlas: rl.Texture2D
window_width: i32
window_height: i32
// orignal resolution of space invaders: 256 x 224 px
setup :: proc(state: ^GameState) {
using state
target_fps = 60
// monitor := rl.GetCurrentMonitor()
window_width = rl.GetScreenWidth()
window_height = rl.GetScreenHeight()
current_frame_time = rl.GetTime()
previous_screen = .TITLE
screen = .TITLE
rl.SetTargetFPS(target_fps)
if !ODIN_DEBUG {
rl.SetExitKey(nil)
} else {
log.info("Built with Odin compiler version: ", ODIN_VERSION)
}
}
update :: proc(state: ^GameState) {
using state
frame_counter += 1
last_frame_time = current_frame_time
current_frame_time = rl.GetTime()
delta_time = current_frame_time - last_frame_time
update_screen(state)
}
target: rl.RenderTexture2D
draw :: proc(state: ^GameState) {
rl.BeginTextureMode(target)
{
draw_screen(state)
}
rl.EndTextureMode()
rl.BeginDrawing()
{
rl.ClearBackground(rl.RAYWHITE)
rl.DrawTexturePro(
target.texture,
{0, 0, f32(target.texture.width), f32(-target.texture.height)},
{
(f32(window_width) - (f32(target.texture.width) * ratio)) / 2,
0,
f32(target.texture.width) * ratio,
f32(target.texture.height) * ratio,
},
{0, 0},
0,
rl.WHITE,
)
}
rl.EndDrawing()
}
main :: proc() {
context.logger = log.create_console_logger()
log.info(state.screen)
rl.InitWindow(1200, 720, "title")
defer rl.CloseWindow()
rl.RestoreWindow()
rl.SetWindowState({.WINDOW_RESIZABLE, .WINDOW_MAXIMIZED})
setup(&state)
state.screen_width = 720
state.screen_height = 520
rl.SetWindowMinSize(state.screen_width, state.screen_height)
target = rl.LoadRenderTexture(state.screen_width, state.screen_height)
defer rl.UnloadRenderTexture(target)
ratio = f32(window_height) / f32(target.texture.height)
texture_atlas_image = rl.LoadImage(TEXTURE_ATLAS_PATH)
texture_atlas = rl.LoadTextureFromImage(texture_atlas_image)
rl.UnloadImage(texture_atlas_image)
log.info("Loaded images")
for !rl.WindowShouldClose() {
if rl.IsWindowResized() {
window_width = rl.GetScreenWidth()
window_height = rl.GetScreenHeight()
ratio = f32(window_height) / f32(target.texture.height)
}
update(&state)
draw(&state)
}
}