Reputation: 91
Similar questions have been asked before, but I'm having trouble wrapping my head around pointers and multidimensional arrays for my specific case. I'm not the best C programmer, but I'm trying to get better. I was wondering if somebody could tell me conceptually what I'm missing and hopefully help me clear up some concepts. I'm building a space invaders type game in c which is going to be run on an embedded platform (i.e. low memory). My embedded device sends output over serial to a computer which outputs the game information to some gui i'm writing in python; the game itself though is being played on the micro controller. The gui interprets this information and draws pictures where I want, but it's just taking data and drawing, nothing more.
The issue I'm having is trying to figure out how to construct the game and how it will be played. Here is my idea:
Get the window resolution from computing platform (this will be the size of the window scaled, though I'm not quite sure to what yet).
Using that scaled resolution create a 2D array which represents the game grid where each element in the array represents a possible location of the player, enemy, bullet, etc...).
Each 2D location (x, y) coordinate can have a multitude of information about that location including but not limited to: the presence of some object, that object's current status (health, other data about it), etc...
At each computing cycle, update the current location and stats of each object on the game map.
To help clarify what I'm saying I've added a crude illustration below. I've created a 2D grid which represents the 'game map'. Imagine this is the grid on which my game will be played. Each place where there is an x is a place that is currently empty of any object. Any place with an o is where some unique object exists, each with different unique characteristics.
My question is how do I create this thing? I'm comfortable with the concept of creating the 2D array, but an array is just a reference to some location where I store some data type. Can I store a pointer to my unique object in say, myArray[2][4]
? And if so, how do I do this? Also, say I store some pointer to my object there, how do I give it multiple attributes? I've create some typdef structs which define the attributes I want (including its current location), but does each o in my map reference this entire structure?
I'm just a bit confused and am looking to clean up my idea. Thank you all for your time!
[x] [x] [x] [x] [x] [x] [x] [x] [x] [x]
[x] [x] [x] [o] [o] [o] [o] [o] [x] [x]
[x] [x] [x] [o] [o] [o] [o] [o] [x] [x]
[x] [x] [x] [x] [x] [o] [x] [x] [x] [x]
[x] [x] [x] [x] [x] [x] [x] [x] [x] [x]
[x] [x] [x] [x] [x] [x] [x] [x] [x] [x]
[x] [x] [x] [x] [x] [x] [x] [x] [x] [x]
[x] [x] [x] [x] [x] [x] [x] [x] [x] [x]
[x] [x] [o] [o] [o] [o] [o] [x] [x] [x]
[x] [x] [x] [x] [x] [o] [x] [x] [x] [x]
Upvotes: 3
Views: 142
Reputation: 12615
Make your array a 2-dimensional array of pointers to a specific kind of struct that you've declared in your program. The structure will contain all the attributes and/or references to related structs. Each element of your matrix (e.g. 2-dimensional array) will contain NULL
or a pointer to a relevant struct.
If your game associates several cells together you can link them to several structs that contain information to associate them among themselves, or just point struct and then determine the surrounding matrix elements through whatever algorithm you see fit to determine the geometry of the relation ship and act accordingly (to the way you've designed your game).
Anytime you want to create a new 'thingy', use malloc(). Use free() to free it and set the reference to NULL when it is freed so you don't try to access a free object or double free (both bad). Freeing NULL is harmless.
Someone suggested a void * linking to different objects where, by convention (your convention if you use the approach) the first field indicates a type. But if you make a generic type like thingy_t, it can differentiate itself from other thingies any way you choose. There are multiple ways to approach it.
typedef struct thingy {
struct thingy *nextThingy;
struct thingy *prevThingy;
char *name;
uint32_t color;
int goldCoinCount;
int isElf;
int annoyedALephrechaun;
int StarFleetApproved;
int rank;
int serialNumber;
float netWorth;
houseType_t *houseType;
struct thingy *friends[];
int prevCoords[2];
.
.
.
} thingy_t;
#define MAX_ROWS 100
#define MAX_COLS 100
thingy_t *playingField[MAX_ROWS][MAX_COLS];
Upvotes: 1