Activity
Mon
Wed
Fri
Sun
Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
What is this?
Less
More

Memberships

Ooga Booga Game Devs

3.1k members β€’ Free

2 contributions to Ooga Booga Game Devs
simple approach to 2D arrays in c
Came across this cool approach to handling 2D arrays in C which I thought might be nice to share. The approach is basically using a 1d of size width*height of the array e.g: say we have a 1d array of our tiles enum: enum TILES tiles[TILES_WIDTH * TILES_HEIGHT]; we can just loop over it with a single index: for(int i = 0; i < TILES_WIDTH * TILES_HEIGHT; i++) { enum TILES tile = tiles[i]; // If we need the x and y values int x = index % TILES_WIDTH; int y = index / TILES_WIDTH; draw_rect(v2(x * tile_size, y * tile_size), v2(tile_size, tile_size), color); } if we want to index into a given x and y coordinate we multiply the y index by the width of the array and add the x index: tiles[y * TILES_WIDTH + x] I've found this approach has been useful for tile/grid based games EDIT: Thanks @Nick H for the suggestion
6 likes β€’ Jul '24
I agree this is a super useful way of handling the map of grid based games, by the way if you have a 1d array of size [width * height] and want to get the x & y from an index, you can just do x = index % width and y = index / width, you dont need to do a float division and floor it because the int division already truncates the decimal place
Gamedev resources
All of the GDC talks from 2022 and before are free on their website. It has talks from industry veterans on anything from programming, game design, narrative talks, etc. https://gdcvault.com/browse/
4
0
1-2 of 2
Nick H
2
10points to level up
@nick-h-7199
learning low level programming

Active 367d ago
Joined Jul 9, 2024