Skip to content

Commit dff3da1

Browse files
committed
e29 basic tilemap collision check
1 parent 4149d8c commit dff3da1

File tree

2 files changed

+241
-35
lines changed

2 files changed

+241
-35
lines changed

src/handmade.rs

+216-33
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ typedef float real32;
1616
typedef double real64;
1717
1818
*/
19+
type bool32 = i32;
1920
use std::convert::TryInto;
2021
use std::ffi::c_void;
2122
use std::ptr::null_mut;
@@ -46,6 +47,27 @@ pub struct GameInput {
4647
pub struct thread_context {
4748
pub place_hodler: i32,
4849
}
50+
51+
struct tile_map {
52+
pub CountX: i32,
53+
pub CountY: i32,
54+
55+
pub UpperLeftX: f32,
56+
pub UpperLeftY: f32,
57+
pub TileWidth: f32,
58+
pub TileHeight: f32,
59+
60+
pub Tiles: *const u32, //look into making it so type implements trait Sized?
61+
}
62+
63+
struct world {
64+
// TODO(casey): Beginner's sparseness
65+
pub TileMapCountX: i32,
66+
pub TileMapCountY: i32,
67+
68+
pub TileMaps: *const tile_map,
69+
}
70+
4971
#[derive(Default)]
5072
pub struct GameControllerInput {
5173
pub is_connected: i32,
@@ -131,6 +153,76 @@ pub struct DebugReadFile {
131153
pub contents: *mut c_void,
132154
}
133155

156+
unsafe fn GetTileMap(World: &world, TileMapX: i32, TileMapY: i32) -> *mut tile_map {
157+
let mut TileMap = 0 as *mut tile_map;
158+
159+
if (TileMapX >= 0)
160+
&& (TileMapX < World.TileMapCountX)
161+
&& (TileMapY >= 0)
162+
&& (TileMapY < World.TileMapCountY.try_into().unwrap())
163+
{
164+
TileMap = World.TileMaps.offset(
165+
(TileMapY * World.TileMapCountX + TileMapX)
166+
.try_into()
167+
.unwrap(),
168+
) as *mut tile_map;
169+
}
170+
171+
return TileMap;
172+
}
173+
174+
unsafe fn GetTileValueUnchecked(TileMap: &tile_map, TileX: i32, TileY: i32) -> u32 {
175+
let TileMapValue = TileMap
176+
.Tiles
177+
.offset((TileY * TileMap.CountX + TileX).try_into().unwrap());
178+
return *TileMapValue;
179+
}
180+
181+
unsafe fn IsTileMapPointEmpty(TileMap: &tile_map, TestX: f32, TestY: f32) -> bool32 {
182+
let mut Empty: bool32 = false as i32;
183+
184+
let PlayerTileX = ((TestX - TileMap.UpperLeftX) / TileMap.TileWidth) as i32;
185+
let PlayerTileY = ((TestY - TileMap.UpperLeftY) / TileMap.TileHeight) as i32;
186+
187+
if (PlayerTileX >= 0)
188+
&& (PlayerTileX < TileMap.CountX)
189+
&& (PlayerTileY >= 0)
190+
&& (PlayerTileY < TileMap.CountY)
191+
{
192+
let TileMapValue = GetTileValueUnchecked(&TileMap, PlayerTileX, PlayerTileY);
193+
Empty = (TileMapValue == 0) as i32;
194+
}
195+
196+
return Empty;
197+
}
198+
199+
unsafe fn IsWorldPointEmpty(
200+
World: &world,
201+
TileMapX: i32,
202+
TileMapY: i32,
203+
TestX: f32,
204+
TestY: f32,
205+
) -> bool32 {
206+
let mut Empty: bool32 = false as bool32;
207+
208+
let TileMap = GetTileMap(World, TileMapX, TileMapY);
209+
if TileMap != null_mut() {
210+
let PlayerTileX = ((TestX - (*TileMap).UpperLeftX) / (*TileMap).TileWidth) as i32;
211+
let PlayerTileY = ((TestY - (*TileMap).UpperLeftY) / (*TileMap).TileHeight) as i32;
212+
213+
if (PlayerTileX >= 0)
214+
&& (PlayerTileX < (*TileMap).CountX)
215+
&& (PlayerTileY >= 0)
216+
&& (PlayerTileY < (*TileMap).CountY)
217+
{
218+
let TileMapValue = GetTileValueUnchecked(&(*TileMap), PlayerTileX, PlayerTileY);
219+
Empty = (TileMapValue == 0) as bool32;
220+
}
221+
}
222+
223+
return Empty;
224+
}
225+
134226
#[no_mangle]
135227
pub extern "C" fn game_update_and_render(
136228
thread: &thread_context,
@@ -139,17 +231,119 @@ pub extern "C" fn game_update_and_render(
139231
mut buffer: &mut GameOffScreenBuffer,
140232
) {
141233
unsafe {
234+
let TILE_MAP_COUNT_X = 17;
235+
let TILE_MAP_COUNT_Y = 9;
236+
//[9][17]
237+
let Tiles00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+
];
284+
285+
// let mut TileMaps[2][2];
286+
let tilemap00 = tile_map {
287+
Tiles: Tiles00.as_ptr() as *mut u32,
288+
CountX: TILE_MAP_COUNT_X,
289+
CountY: TILE_MAP_COUNT_Y,
290+
UpperLeftX: -30.0,
291+
UpperLeftY: 0.0,
292+
TileWidth: 60.0,
293+
TileHeight: 60.0,
294+
};
295+
let mut TileMaps = [[tilemap00]];
296+
297+
298+
/* TileMaps[0][0].Tiles = (uint32 *)Tiles00;
299+
300+
TileMaps[0][1] = TileMaps[0][0];
301+
TileMaps[0][1].Tiles = (uint32 *)Tiles01;
302+
303+
TileMaps[1][0] = TileMaps[0][0];
304+
TileMaps[1][0].Tiles = (uint32 *)Tiles10;
305+
306+
TileMaps[1][1] = TileMaps[0][0];
307+
TileMaps[1][1].Tiles = (uint32 *)Tiles11;
308+
309+
tile_map *TileMap = &TileMaps[0][0];
310+
*/
311+
let TileMap = &TileMaps[0][0];
312+
let World = world {
313+
TileMapCountX: 2,
314+
TileMapCountY: 2,
315+
316+
TileMaps: TileMaps.as_ptr() as *mut tile_map,
317+
};
318+
319+
let mut PlayerWidth = 0.75 * TileMap.TileWidth as f32;
320+
let mut PlayerHeight = TileMap.TileHeight;
142321
let mut game_state = memory.permanent_storage as *mut GameState;
322+
143323
if memory.is_initalized == 0 {
324+
(*game_state).PlayerX = 150.0;
325+
(*game_state).PlayerY = 150.0;
144326
memory.is_initalized = 1;
145327
}
328+
329+
let PlayerWidth = 0.75 * TileMap.TileWidth;
330+
let PlayerHeight = TileMap.TileHeight;
331+
332+
let UpperLeftX: f32 = -30.0;
333+
let UpperLeftY: f32 = 0.0;
334+
let TileWidth: f32 = 60.0;
335+
let TileHeight: f32 = 60.0;
336+
337+
let width = buffer.width;
338+
let height = buffer.height;
339+
146340
for controller_index in 0..input.controllers.len() {
147341
let controller = &mut input.controllers[controller_index];
148342
if controller.is_analog != 0 {
149343
} else {
150344
// NOTE(casey): Use digital movement tuning
151-
let mut dPlayerX = 0.0; // pixels/second
152-
let mut dPlayerY = 0.0; // pixels/second
345+
let mut dPlayerX: f32 = 0.0; // pixels/second
346+
let mut dPlayerY: f32 = 0.0; // pixels/second
153347

154348
if controller.move_up().ended_down != 0 {
155349
dPlayerY = -1.0;
@@ -167,30 +361,19 @@ pub extern "C" fn game_update_and_render(
167361
dPlayerY *= 64.0;
168362

169363
// 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;
172-
};
364+
let NewPlayerX = (*game_state).PlayerX + input.dtForFrame * dPlayerX as f32;
365+
let NewPlayerY = (*game_state).PlayerY + input.dtForFrame * dPlayerY as f32;
366+
367+
if IsTileMapPointEmpty(&TileMap, NewPlayerX - 0.5 * PlayerWidth, NewPlayerY) != 0
368+
&& IsTileMapPointEmpty(&TileMap, NewPlayerX + 0.5 * PlayerWidth, NewPlayerY)
369+
!= 0
370+
&& IsTileMapPointEmpty(&TileMap, NewPlayerX, NewPlayerY) != 0
371+
{
372+
(*game_state).PlayerX = NewPlayerX;
373+
(*game_state).PlayerY = NewPlayerY;
374+
}
375+
}
173376
}
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;
191-
192-
let width = buffer.width;
193-
let height = buffer.height;
194377

195378
DrawRectangle(
196379
&mut buffer,
@@ -203,28 +386,28 @@ pub extern "C" fn game_update_and_render(
203386
0.1,
204387
);
205388

206-
for (row_index, row) in TileMap.iter().enumerate() {
389+
for (row_index, row) in Tiles00.iter().enumerate() {
207390
for (column_index, column) in row.iter().enumerate() {
208-
let tileID = column;
391+
let tileID = GetTileValueUnchecked(&TileMap, column_index.try_into().unwrap(), row_index.try_into().unwrap());
209392
let mut Gray = 0.5;
210393
match tileID {
211394
1 => Gray = 1.0,
212395
_ => {}
213396
}
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;
397+
let mut MinX = TileMap.UpperLeftX + (column_index as f32) * TileWidth;
398+
let mut MinY = TileMap.UpperLeftY + (row_index as f32) * TileHeight;
399+
let mut MaxX = MinX + TileMap.TileWidth;
400+
let mut MaxY = MinY + TileMap.TileHeight;
218401
DrawRectangle(buffer, MinX, MinY, MaxX, MaxY, Gray, Gray, Gray);
219402
}
220403
}
221404
let PlayerR = 1.0;
222405
let PlayerG = 1.0;
223406
let mut PlayerB = 0.0;
224-
let mut PlayerWidth = 0.75 * TileWidth as f32;
225-
let mut PlayerHeight = TileHeight;
407+
226408
let mut PlayerLeft = (*game_state).PlayerX - 0.5 * PlayerWidth;
227409
let mut PlayerTop = (*game_state).PlayerY - PlayerHeight as f32;
410+
228411
DrawRectangle(
229412
buffer,
230413
PlayerLeft,

src/win32_handmade.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ use winapi::um::fileapi::ReadFile;
4545
use winapi::um::fileapi::WriteFile;
4646
use winapi::um::fileapi::CREATE_ALWAYS;
4747
use winapi::um::fileapi::OPEN_EXISTING;
48+
use winapi::um::wingdi::PatBlt;
49+
use winapi::um::wingdi::BLACKNESS;
4850

4951
use winapi::um::handleapi::CloseHandle;
5052
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
@@ -818,12 +820,33 @@ fn win32_update_window(
818820
window_height: i32,
819821
buffer: &Win32OffScreenBuffer,
820822
) {
823+
let OffsetX = 10;
824+
let OffsetY = 10;
825+
821826
unsafe {
822-
//blit 1 to 1 for learning how to code renderer, no scaling
823-
StretchDIBits(
827+
PatBlt(device_context, 0, 0, window_width, OffsetY, BLACKNESS);
828+
PatBlt(
824829
device_context,
825830
0,
831+
OffsetY + buffer.height,
832+
window_width,
833+
window_height,
834+
BLACKNESS,
835+
);
836+
PatBlt(device_context, 0, 0, OffsetX, window_height, BLACKNESS);
837+
PatBlt(
838+
device_context,
839+
OffsetX + buffer.width,
826840
0,
841+
window_width,
842+
window_height,
843+
BLACKNESS,
844+
);
845+
//blit 1 to 1 for learning how to code renderer, no scaling
846+
StretchDIBits(
847+
device_context,
848+
OffsetX,
849+
OffsetY,
827850
buffer.width,
828851
buffer.height,
829852
0,

0 commit comments

Comments
 (0)