Changing BMAP's dynamically

Posted By: Yking

Changing BMAP's dynamically - 10/07/14 09:29

Hello everyone,

I currently have a minor problem, but I can't find any solution for it, maybe it is some kind of Beginners-Problem.

Let's assume I want a game with 100 items in it, I want to code in a non-copy-paste way. I made an Array for Itemnames and one for BMAP's. So now I can use the Index of my array as an "ID-code". Item[1] has Picture[1], that sounds logical to me.

So, my Arrays are global, I want to access them easily, I created a function to write some data into my index-numbers, also not a problem (and when I test them via printf(xxx); then I get the correct information aswell (Name, path to the picture).

Okay, here is the problem:
I am calling another function (it comes last), which should use a panel to display the BMAP. Unfortunately it does not work.

It all looks like this:
Quote:

STRING* item_name[100];
BMAP* item_bmap[100];

function make_items()
{
item_name[1] = "Potion";
item_bmap[1] = "items/drink/potion.tga";
}

function display_item()
{
PANEL* test = {layer = 4; flags = VISIBLE; pos_x = 200; pos_y = 200; bmap = item_bmap[1];}
}


The error is, that the program doesnt know what this Bitmap is, so I thought I need to declare some other (empty) Bitmap first to avoid errors, and then just before I want to display it, I change the bitmap-path, so I get the BMAP I want.
Here is what I came up with:

Quote:

STRING* item_name[100];
BMAP* item_bmap[100];
BMAP* placeholder = "items/empty.tga";

function make_items()
{
item_name[1] = "Potion";
item_bmap[1] = "items/drink/potion.tga";
}

function display_item()
{
placeholder = item_bmap[1];
printf("placeholder ");
PANEL* test = {layer = 4; flags = VISIBLE; pos_x = 200; pos_y = 200; bmap = placeholder;}
}


To sum it up: The placeholder gets overwritten and for debugging-purposes I do a "printf()".
The message I get on my screen is absolutely right: Its the path to the .tga that I want to have BUT
my panel still gets the "placeholder-Bitmap".

So I think, my question is: How do I correctly change the bitmap of my panel dynamically, or is there a better solution for my problem?

Thanks for all help in advance laugh
Posted By: Superku

Re: Changing BMAP's dynamically - 10/07/14 09:45

Why do you declare the panel inside a function?
Either create it globally as well or dynamically via pan_create() - or alternatively use draw_quad to draw your bitmaps (above all other panels, though).
You can change the bmap of a panel at any time like this:
pnl_test.bmap = item_bmap[1];
(Btw. indices start at 0, not 1, but I guess you know that.)
You can declare the panel without a SHOW (not VISIBLE) flag and only set it when you actually want to see its content.
Posted By: HellThunder

Re: Changing BMAP's dynamically - 10/07/14 09:57

Try this to initialize a STRING or a BMAP.

Code:
function make_items()
{
item_name[0] = str_create("Potion");
item_bmap[0] = bmap_create("items/drink/potion.tga");
}



Please start counting with 0. If you have an array with a length of 100 - the first variable is always 0 and the last one 99. Sounds a little bit strange but do it to prevent memory leaks.


You don't have to use the directory every time if you want to create a new bmap.

Use
Code:
#define PRAGMA_PATH ".\\items\\drink";


at the beginning of your script. Then you can use every file inside this folder by typing just the name of it.

Code:
item_bmap[0] = bmap_create("potion.tga");



Regards,
HellThunder
Posted By: Yking

Re: Changing BMAP's dynamically - 10/07/14 19:27

Thanks for all the help, I got it working now laugh .

This panel I talked about is Debug-Only, just so I can see if the bmap really changes.
I didn't use the index 0 because I reserved that for an "no item state" laugh

I used HellThunders method of doing
Quote:

item_name[0] = str_create("Potion");
item_bmap[0] = bmap_create("items/drink/potion.tga");


and used Superkus
Quote:

pnl_test.bmap = item_bmap[1];

to change the panelbmap... and it worked grin !

Excellent help, you guys laugh !
Posted By: WretchedSid

Re: Changing BMAP's dynamically - 10/07/14 22:46

Originally Posted By: HellThunder
Please start counting with 0. If you have an array with a length of 100 - the first variable is always 0 and the last one 99. Sounds a little bit strange but do it to prevent memory leaks.

This has nothing to do with memory leaks. If you don't touch the first element in an array, it won't do anything. It does decrease readability and maintainability, especially for outside developers, but tough luck for them I suppose.

The real reason you start with index 0 is because otherwise you are simply wasting an element. And are probably overwriting memory at the end.

The reason why it is 0 is also not strange in any way shape or form, the index is simply an offset into the array. The first element logically has the offset 0 because it is at the very beginning.
© 2024 lite-C Forums