added different scaling, breaking change

This commit is contained in:
Stefan Stefanov 2024-02-10 23:04:35 +02:00
parent 8bc4572ae3
commit 799e5ad9eb
4 changed files with 92 additions and 57 deletions

View file

@ -6,9 +6,6 @@ import glm "core:math/linalg/glsl"
import rl "vendor:raylib"
// orignal resolution of space invaders
// 256 x 224 px
TEXTURE_ATLAS_PATH :: "./assets/texture_atlas.png"
DEBUG_MODE :: false
@ -19,55 +16,36 @@ GameEndType :: enum {
AliensReachedPlayer,
}
GameState :: struct {
// window
target_fps: c.int,
title: cstring,
screen_width: c.int,
screen_height: c.int,
// frame stats
frame_counter: int,
current_frame_time: f64,
last_frame_time: f64,
delta_time: f64,
// game vars
screen: GameScreen,
previous_screen: GameScreen,
last_frame_screen: GameScreen,
game_end: GameEndType,
reset_game: bool,
aliens: #soa[ALIENS]Alien,
bullets: #soa[MAX_BULLETS]Bullet,
bullet_index: int,
player_last_time_fired: f64,
player_pos: glm.vec2,
player_health: c.int,
player_score: c.int,
player_high_score: c.int,
shuffle_dir: ShuffleDirection,
last_shuffle_dir: ShuffleDirection,
}
state: GameState
ratio: i32
texture_atlas_image: rl.Image
texture_atlas: rl.Texture2D
camera := rl.Camera2D {
zoom = 2,
zoom = GLOBAL_SPRITE_SCALE,
}
// orignal resolution of space invaders: 256 x 224 px
setup :: proc(state: ^GameState) {
using state
target_fps = 60
screen_width = 800 * 2
screen_height = 600 * 2
title = "Space Invaders (raylib+odin-lang edition)"
monitor := rl.GetCurrentMonitor()
screen_width = rl.GetMonitorWidth(monitor)
screen_height = rl.GetMonitorHeight(monitor)
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) {
@ -81,13 +59,31 @@ update :: proc(state: ^GameState) {
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.BeginMode2D(camera)
draw_screen(state)
rl.EndMode2D()
{
rl.ClearBackground(rl.RAYWHITE)
rl.DrawTexturePro(
target.texture,
{0, 0, f32(target.texture.width), f32(-target.texture.height)},
{
f32((state.screen_width - (target.texture.width * ratio)) / 2),
0,
f32(target.texture.width * ratio),
f32(target.texture.height * ratio),
},
{0, 0},
0,
rl.WHITE,
)
}
rl.EndDrawing()
}
@ -96,16 +92,18 @@ main :: proc() {
log.info(state.screen)
rl.InitWindow(1200, 720, "title")
defer rl.CloseWindow()
rl.RestoreWindow()
rl.SetWindowState({.WINDOW_RESIZABLE, .WINDOW_MAXIMIZED})
setup(&state)
rl.InitWindow(state.screen_width, state.screen_height, state.title)
defer rl.CloseWindow()
if !ODIN_DEBUG {
rl.SetExitKey(nil)
} else {
log.info("Built with Odin compiler version: ", ODIN_VERSION)
}
width: i32 = 720;height: i32 = 520
target = rl.LoadRenderTexture(width, height)
defer rl.UnloadRenderTexture(target)
ratio = i32(state.screen_height / target.texture.height)
texture_atlas_image = rl.LoadImage(TEXTURE_ATLAS_PATH)
texture_atlas = rl.LoadTextureFromImage(texture_atlas_image)

View file

@ -9,7 +9,6 @@ GameScreen :: enum {
ENDING,
}
update_screen :: proc(state: ^GameState) {
using state
@ -53,25 +52,29 @@ draw_screen :: proc(state: ^GameState) {
{
rl.DrawRectangle(0, 0, state.screen_width, state.screen_height, rl.WHITE)
rl.DrawCircle(0, 0, 20, rl.RED)
rl.DrawTexturePro(
texture_atlas,
{LOGO_TO[0].x, LOGO_TO[0].y, LOGO_TO[1].x, LOGO_TO[1].y},
{
f32(screen_width / GLOBAL_SPRITE_SCALE) / 2,
f32(screen_height / GLOBAL_SPRITE_SCALE) / 2,
f32(screen_width / GLOBAL_SPRITE_SCALE),
f32(screen_height / GLOBAL_SPRITE_SCALE),
LOGO_TO[1].x * 4,
LOGO_TO[1].y * 4,
},
{LOGO_TO[1].x * 2, LOGO_TO[1].y * 2},
{
f32(screen_width / GLOBAL_SPRITE_SCALE),
f32(screen_height / GLOBAL_SPRITE_SCALE),
},
0,
rl.GREEN,
)
text : cstring = "PRESS ENTER TO START GAME"
size := rl.MeasureText(text, 20)
rl.DrawText(
text: cstring = "PRESS ENTER TO START GAME"
size := rl.MeasureText(text, 20)
rl.DrawText(
text,
(screen_width/4) - (size/2),
(screen_height / 2)- 20,
(screen_width / 4) - (size / 2),
(screen_height / 2) - 20,
20,
rl.DARKGREEN,
)

View file

@ -9,6 +9,7 @@ LOGO_TO :: [2]glm.vec2{{0, 0}, {128-(SPRITE_CELL * 2), SPRITE_CELL * 2}}
GLOBAL_SPRITE_SCALE :: 2
SPRITE_CELL :: 16
SPRITE :: SPRITE_CELL * GLOBAL_SPRITE_SCALE
ALIEN_ROWS :: 5
ALIENS_PER_ROW :: 11

33
state.odin Normal file
View file

@ -0,0 +1,33 @@
package space_invaders
import "core:c"
import glm "core:math/linalg/glsl"
GameState :: struct {
// window
target_fps: c.int,
title: cstring,
screen_width: c.int,
screen_height: c.int,
// frame stats
frame_counter: int,
current_frame_time: f64,
last_frame_time: f64,
delta_time: f64,
// game vars
screen: GameScreen,
previous_screen: GameScreen,
last_frame_screen: GameScreen,
game_end: GameEndType,
reset_game: bool,
aliens: #soa[ALIENS]Alien,
bullets: #soa[MAX_BULLETS]Bullet,
bullet_index: int,
player_last_time_fired: f64,
player_pos: glm.vec2,
player_health: c.int,
player_score: c.int,
player_high_score: c.int,
shuffle_dir: ShuffleDirection,
last_shuffle_dir: ShuffleDirection,
}