Skip to content

Commit 4149d8c

Browse files
committed
ep27 drawing a tilemap
1 parent 6c88f49 commit 4149d8c

File tree

2 files changed

+88
-8
lines changed

2 files changed

+88
-8
lines changed

Diff for: src/handmade.rs

+86-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub struct GameInput {
3939
pub MouseX: i32,
4040
pub MouseY: i32,
4141
pub MouseZ: i32,
42-
pub SecondsToAdvanceOverUpdate: f32,
42+
pub dtForFrame: f32,
4343
pub controllers: [GameControllerInput; 5],
4444
}
4545
#[derive(Default)]
@@ -104,7 +104,10 @@ pub struct GameButtonState {
104104
pub ended_down: i32,
105105
}
106106
#[derive(Default)]
107-
pub struct GameState {}
107+
pub struct GameState {
108+
pub PlayerX: f32,
109+
pub PlayerY: f32,
110+
}
108111
pub struct GameMemory {
109112
pub is_initalized: i32,
110113
pub permanent_storage_size: u64,
@@ -144,8 +147,47 @@ pub extern "C" fn game_update_and_render(
144147
let controller = &mut input.controllers[controller_index];
145148
if controller.is_analog != 0 {
146149
} else {
150+
// NOTE(casey): Use digital movement tuning
151+
let mut dPlayerX = 0.0; // pixels/second
152+
let mut dPlayerY = 0.0; // pixels/second
153+
154+
if controller.move_up().ended_down != 0 {
155+
dPlayerY = -1.0;
156+
}
157+
if controller.move_down().ended_down != 0 {
158+
dPlayerY = 1.0;
159+
}
160+
if controller.move_left().ended_down != 0 {
161+
dPlayerX = -1.0;
162+
}
163+
if controller.move_right().ended_down != 0 {
164+
dPlayerX = 1.0;
165+
}
166+
dPlayerX *= 64.0;
167+
dPlayerY *= 64.0;
168+
169+
// TODO(casey): Diagonal will be faster! Fix once we have vectors :)
170+
(*game_state).PlayerX += input.dtForFrame * dPlayerX as f32;
171+
(*game_state).PlayerY += input.dtForFrame * dPlayerY as f32;
147172
};
148173
}
174+
//[9][17]
175+
let TileMap = [
176+
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
177+
[1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
178+
[1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1],
179+
[1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
180+
[0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
181+
[1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1],
182+
[1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1],
183+
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
184+
[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
185+
];
186+
187+
let UpperLeftX: f32 = -30.0;
188+
let UpperLeftY: f32 = 0.0;
189+
let TileWidth: f32 = 60.0;
190+
let TileHeight: f32 = 60.0;
149191

150192
let width = buffer.width;
151193
let height = buffer.height;
@@ -156,9 +198,43 @@ pub extern "C" fn game_update_and_render(
156198
0.0,
157199
width as f32,
158200
height as f32,
159-
0x00FF00FF,
201+
1.0,
202+
0.0,
203+
0.1,
204+
);
205+
206+
for (row_index, row) in TileMap.iter().enumerate() {
207+
for (column_index, column) in row.iter().enumerate() {
208+
let tileID = column;
209+
let mut Gray = 0.5;
210+
match tileID {
211+
1 => Gray = 1.0,
212+
_ => {}
213+
}
214+
let mut MinX = UpperLeftX + (column_index as f32) * TileWidth;
215+
let mut MinY = UpperLeftY + (row_index as f32) * TileHeight;
216+
let mut MaxX = MinX + TileWidth;
217+
let mut MaxY = MinY + TileHeight;
218+
DrawRectangle(buffer, MinX, MinY, MaxX, MaxY, Gray, Gray, Gray);
219+
}
220+
}
221+
let PlayerR = 1.0;
222+
let PlayerG = 1.0;
223+
let mut PlayerB = 0.0;
224+
let mut PlayerWidth = 0.75 * TileWidth as f32;
225+
let mut PlayerHeight = TileHeight;
226+
let mut PlayerLeft = (*game_state).PlayerX - 0.5 * PlayerWidth;
227+
let mut PlayerTop = (*game_state).PlayerY - PlayerHeight as f32;
228+
DrawRectangle(
229+
buffer,
230+
PlayerLeft,
231+
PlayerTop,
232+
PlayerLeft + PlayerWidth,
233+
PlayerTop + PlayerHeight as f32,
234+
PlayerR,
235+
PlayerG,
236+
PlayerB,
160237
);
161-
DrawRectangle(&mut buffer, 10.0, 10.0, 40.0, 40.0, 0x0000FFFF);
162238
}
163239
}
164240

@@ -168,7 +244,9 @@ unsafe fn DrawRectangle(
168244
RealMinY: f32,
169245
RealMaxX: f32,
170246
RealMaxY: f32,
171-
Color: u32,
247+
R: f32,
248+
G: f32,
249+
B: f32,
172250
) {
173251
// TODO(casey): Floating point color tomorrow!!!!!!
174252

@@ -193,6 +271,9 @@ unsafe fn DrawRectangle(
193271
MaxY = Buffer.height;
194272
}
195273

274+
let Color = ((R * 255.0).round() as u32) << 16
275+
| ((G * 255.0).round() as u32) << 8
276+
| ((B * 255.0).round() as u32) << 0;
196277
let mut Row = Buffer
197278
.memory
198279
.offset((MinX * Buffer.bytes_per_pixel + MinY * Buffer.pitch) as isize)

Diff for: src/win32_handmade.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1199,8 +1199,6 @@ unsafe fn win32_get_seconds_elasped(start: LARGE_INTEGER, end: LARGE_INTEGER) ->
11991199
}
12001200
fn main() {
12011201
unsafe {
1202-
let t = b"asdf\0";
1203-
dbg!(from_utf8(t));
12041202
winmain();
12051203
}
12061204
}
@@ -1489,7 +1487,6 @@ pub unsafe extern "system" fn winmain() {
14891487
{
14901488
let mut old_input = GameInput::default();
14911489
let mut new_input = GameInput::default();
1492-
new_input.SecondsToAdvanceOverUpdate = target_seconds_per_frame;
14931490
let mut last_counter = win32_get_wall_clock();
14941491
let mut FlipWallClock = win32_get_wall_clock();
14951492

@@ -1506,6 +1503,8 @@ pub unsafe extern "system" fn winmain() {
15061503
);
15071504
let mut last_cycle_count = _rdtsc();
15081505
while RUNNING {
1506+
new_input.dtForFrame = target_seconds_per_frame;
1507+
15091508
let new_dll_write_time =
15101509
win32_get_last_write_time(source_game_code_dll_file_name_full_path);
15111510
if CompareFileTime(&new_dll_write_time, &game.dll_last_write_time) != 0 {

0 commit comments

Comments
 (0)