I am using DEV-C++ 5 on Windows, XP first off. Now, in general, I know how a video game works. You need an engine to govern the game, maps in order to create the worlds, you need scenario data in order to control events in the game, you need entities like characters, items, and enemies, you need a battle system, and most importantly, you need something to clean up memory after the program. All this is put into a loop and it makes a video game.
Right now I am in a process of creating an engine, seeing as a "blank" engine can be used to make an array of different games, that is what I am working on. I am currently learning how to apply graphics and layers, so for the time being I am starting a text based video game, and then I will improve the engine from there.
My engine will be used to make RPG type games, and the current piece of code I am working on is classically known as the "shop" menu. I have inventory slots made, and currently I cannot sell items, but I am halfway done with finishing the code for buying items. In summary what the code currently does is as follows. You can open the shop menu and then choose "buy". The program will generate a list with 16 "slots", each slot being an integer. The items are also integers, and have a set value, as to indicate a set numeric code that lets the program determine the different stats for each item.
Example:
Code:
//Slots do not have a set value, they are manipulated throughout the game.
int slot1;
int slot2;
int slot3;
//Items have a set integer to be used as an item code.
int daggar = 1;
int mace = 2; |
Before the do while loop inclosing the code for the shop starts, the different slots are assigned a value. Then when the shop code runs the value of the slots are compared to the values of the different items. When the computer finds a match, it will then display the stats of the item indicated by the item code. This is done by comparing one slot value at a time to each item value using if and else if statements, with the else statement naturally being a user input error message.
Example:
Code:
slot1 = 3;
if(slot1 == 1)
{
//Display Dagger Stats
}
else if(slot2 == 1)
{
//Display Mace Stats
}
else if(slot3 == 1)
{
//Display Helm Stats
} |
This code is then wrapped into a series of #define statements and made into a single command. Then the game will populate your list with items and item stats according to the item code, your gold, and then prompts for user input. The item list is numbered, and the user inputs the number that correlates to the item they desire to buy. The program then checks the slot values again, compares them to the item code, checks to make sure you have enough gold, deducts your gold, adds the item, and then, if you are not buying potions, sets a switch to make sure you cannot have the same item twice (Personal preference for a small game.).
Example:
Code:
(Populate list. Prompt for input.)
(User input of 2)
if(input == "1")
{
//Buy Item in Slot 1
}
else if(input == "2" )
{
//Buy Item in Slot 2
} |
Now, my problem, basically, is that I am using too much memory to keep track of these lists, I believe the memory it takes to store, compare and keep track of the slots is what has become my problem, because when I remove the code for actually buying the items (Not populating the list that works fine.), the program will compile. Now that I have explained how my item system works, I can ask my question in a manner that makes sense. I will go down the list with two different snips and bits of my code first.
"itemhandler.h"
Code:
#define BUY1 if(slot1 == 1){ //Buy Dagger } else if(slot1 == 2)... (21 More if statements checking slot1)
#define BUY2 if(slot2 == 1){ //Buy Dagger } else if(slot2 == 2)... (21 More if statements checking slot2)
#define BUY3 if(slot3 == 1){ //Buy Dagger } else if(slot3 == 2)... (21 More if statements checking slot3)
//Ect. ect. (Goes up to BUY16) |
Then in order to implement the code to check the items and buy them
"main.cpp"
Code:
#include "itemhandler.h"
//Other #include's
//...
string input;
//.....
slot1 = 3;
slot2 = 7;
slot3 = 11;
//Ect. ect. until values have been set for integers slot1 - slot16
flag3 = 1;
do
{
//Ask user to buy, sell, or cancel. Assuming user chooses to buy, then populate the list, show gold and wait for user input.
cin>>input;
if(input == "1")
{
BUY1
}
else if(input == "2")
{
BUY2
}
else if(input == "3")
{
BUY3
}
//Ect. ect. List repeats pattern all the way down to BUY16
//More stuff, if user chooses to exit flag = 0
}while (flag == 1); |
|
As you can imagine, even just #define BUY1 alone is going to be a lengthy line of code, seeing as there are 23 different if statements that's used to check it. The idea behind this system is that when I get done, all I will have to do is plug the desired item integer values into the list, and the game will automatically run through every item possibility and display and keep track of the items. That way all I have to do in each world is put in the lines of code for the shop and plug in my numbers to set the different selections each "shop keeper" offers.
!!!!! NOW HERE IS MY PROBLEM AND QUESTION !!!!
When I place the code I have summarized in the previous snips of itemhandler.h and main.cpp into the program, the compiler will not make an executable. Instead I get the following error.
out of memory allocating 65536 bytes
[Build Error] [main.o] Error 1 |
|
|
Now, from what I have researched, something needs to go on its own block of memory, and then it has to be freed when we are done with the new memory block, but I am not sure if free(); can be used with integers. If I use something like this I also get an error message.
Example:
Code:
if(input == "1")
{
slot1 = malloc(sizeof(slot1));
BUY1
free(slot1);
} |
The error message ends up as so...
(The lines specified below containing errors reference the lines in the code above, not the lines in the original code. I did this to be sure you can specify which commands the errors are assigned to.)
Line 4 |||| invalid conversion from (void*) to (int)
Line 6 |||| invalid conversion from (int) to (void*) |
Am I using malloc() right? Should I use malloc() at all? Should I use calloc()? Is there a different, simpler way to clear memory? Would my slots actually be the problem as I have assumed, or could it be something else I am storing?
Any information on getting this code to work is greatly appreciated! Thank you in advanced!
- @pavil