Well, here I am again XD
I think I've solved the "terrain" question running a matrix with referrence to the terrain to render.
At now I'm trying to manage my "spaghetti-code" inventory system.
Let me say that the sprites of inventory slots are 35x35 px, and each slot can be: empty, hovered by cursor, pressed and selected.
I would like to keep the graphic of the Selected slot until I press again the right mouse button.
The code I've written doesn't show me a persistent "selected" state.
#include "DarkGDK.h"
//sample size for rows and columns of the inventory matrix
#define inv_righe 3
#define inv_colonne 5
// var declarations
int contatore = 0;
int click = 0;
int mx = 0;
int my = 0;
int i = 1;
int j = 1;
int num_slot = 0;
struct SLOT
{
int id_slot;
int pos_slot_x; //x position of slot[i][j]
int pos_slot_y;
bool slot_vuoto;
char *gfx_slot; //path of empty slot image
char *gfx_slot_h; //path of hovered slot image
char *gfx_slot_p; //path of clicked slot image
char *gfx_slot_s; //path of seleted slot image
int id_ogg; //id of object contained
int qta_ogg; //quantity of objects contained in slot
int stato_slot; //0 - simple empty slot; 1 - hovered; 2 - clicked; 3 - selected;
};
// struct of each object (work in progress)
struct OGGETTO
{
int id_ogg;
char *gfx_ogg;
char *gfx_ogg_h;
char *gfx_ogg_p;
char *gfx_ogg_s;
char *nome_ogg;
char *desc_ogg;
};
//inventory matrix
SLOT matrice_inventario [inv_righe][inv_colonne];
OGGETTO oggetti [2];
//initialize function: id = 0 for the empty slot, path reference for each status of the slot, name of the object and it's description
void Inizializza (void)
{
oggetti[1].id_ogg = 0;
oggetti[1].gfx_ogg = "media\\slot_vuoto.png";
oggetti[1].gfx_ogg_h = "media\\slot_vuoto_h.png";
oggetti[1].gfx_ogg_s = "media\\slot_vuoto_s.png";
oggetti[1].gfx_ogg_p = "media\\slot_vuoto_p.png";
oggetti[1].nome_ogg = "Slot vuoto";
oggetti[1].desc_ogg = "Questo sloto non contiene oggetti";
for (i = 1; i <= inv_righe; i++)
{
for (j = 1; j <= inv_colonne; j++)
{
contatore = contatore + 1;
matrice_inventario [i][j].id_slot = contatore;
matrice_inventario [i][j].pos_slot_x = 35 * j;
matrice_inventario [i][j].pos_slot_y = 35 * i;
matrice_inventario [i][j].slot_vuoto = true;
matrice_inventario [i][j].gfx_slot = oggetti[1].gfx_ogg;
matrice_inventario [i][j].gfx_slot_h = oggetti[1].gfx_ogg_h;
matrice_inventario [i][j].gfx_slot_p = oggetti[1].gfx_ogg_p;
matrice_inventario [i][j].gfx_slot_s = oggetti[1].gfx_ogg_s;
matrice_inventario [i][j].id_ogg = oggetti[1].id_ogg;
matrice_inventario [i][j].qta_ogg = 0;
matrice_inventario [i][j].stato_slot = 0; //default state of empty slot = simple empty slot image
}
}
}
//video output of inventory
void Disegna_inv (void)
{
for (i = 1; i <= inv_righe; i++)
{
for (j = 1; j <= inv_colonne; j++)
{
if ((mx >= matrice_inventario[i][j].pos_slot_x) && (mx < matrice_inventario[i][j].pos_slot_x + 35) && (my >= matrice_inventario[i][j].pos_slot_y) && (my < matrice_inventario[i][j].pos_slot_y + 35))
{
matrice_inventario [i][j].stato_slot = 1; //if mouse on slot area, then change status to "hovered"
if (dbMouseClick () == 1)
{
matrice_inventario [i][j].stato_slot = 2; //if left click on hovered slot, then "pressed status"
}
if (dbMouseClick () == 2) //if right click, change status to "selected"
{
matrice_inventario [i][j].stato_slot = 3;
}
}
else
{
if (matrice_inventario[i][j].stato_slot != 3) //if status is "not-selected" draw "simple empty slot
{
matrice_inventario[i][j].stato_slot = 0;
}
else //it should be the "status = 3", selected
{
if (dbMouseClick() == 2) //if right-clicked again, return to "empty slot"
{
matrice_inventario[i][j].stato_slot = 0;
}
}
}
// now using a counter to give a reference to the dbSprite
num_slot = num_slot + 1;
if ((matrice_inventario [i][j].stato_slot)== 0)
{
dbSprite( num_slot, matrice_inventario[i][j].pos_slot_x, matrice_inventario[i][j].pos_slot_y, 1);
}
if ((matrice_inventario [i][j].stato_slot)== 1)
{
dbSprite( num_slot, matrice_inventario[i][j].pos_slot_x, matrice_inventario[i][j].pos_slot_y, 2);
}
if ((matrice_inventario [i][j].stato_slot)== 2)
{
dbSprite( num_slot, matrice_inventario[i][j].pos_slot_x, matrice_inventario[i][j].pos_slot_y, 3);
}
if ((matrice_inventario [i][j].stato_slot)== 3)
{
dbSprite( num_slot, matrice_inventario[i][j].pos_slot_x, matrice_inventario[i][j].pos_slot_y, 4);
}
dbSetSprite( num_slot, 0, 0);
}
}
}
//main
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
dbSetDisplayMode (800, 600, 32);
dbLoadImage("media\\bg.bmp", 100); // a 1x1 px black image, because I'm not able to clear the screen and set it to "black"
//dbInk and dbBox with black color won't work, cannot understand why XD
dbSprite(100, 0, 0, 100);
dbSetSprite(100, 0, 0);
dbSizeSprite (100, 800, 600);
dbLoadImage("media\\slot_vuoto.png", 1); //slot empty
dbLoadImage("media\\slot_vuoto_h.png", 2); //slot empty hover
dbLoadImage("media\\slot_vuoto_s.png", 3); //slot empty selected
dbLoadImage("media\\slot_vuoto_p.png", 4); //slot vuoto pressed
Inizializza(); //initialize beta inventory
dbDrawSpritesFirst ();
// our main loop
while ( LoopGDK ( ) )
{
mx = dbMouseX ();
my = dbMouseY ();
dbText(0, 0, "Inventario 1");
Disegna_inv();
// update the screen
dbSync ( );
}
// return back to windows
return;
}