efficient way to draw mode 3 sprites?

This forum is for artists to post their music and graphics or discuss artistic matters that could be used in Uzebox games.
Post Reply
User avatar
schepers_cp
Posts: 125
Joined: Tue Feb 04, 2014 9:48 pm
Location: netherlands
Contact:

efficient way to draw mode 3 sprites?

Post by schepers_cp » Thu Feb 06, 2014 9:49 pm

hi all, i wonder, i've got some graphics working atm in video mode 3, but how can i assign a unique sprite-id to them, so if i want to move them, only the given sprite moves?

also, i can't seem to get more than 4 sprites at the same time... :S here's my code to generate sprites:

Code: Select all

int unts = 0;
unsigned int unit_id = 0;
int units_create = 30;
unsigned int unit_vars[8] = {1,1,50,1,8,8,8,8}; //side(1/2: player/ai), type (1/2/3: light/heavy/resource), health, speed, x, y, dest x, desty

int main() {
	ClearVram();
	game_start(); //for unit amount debug stuff
}


void create_sprite( unsigned int unt_id, unsigned int id, unsigned int side,unsigned int unt_x, unsigned int unt_y) {
	if (side == 1) {
		//it's the player
		MapSprite2(0,tank_side_normal,0);
		MoveSprite(0,unt_x,unt_y,8,8);
	} else {
		//do ai sprites
	}
}

void create_unit(int side, int type, int health, int speed, int xx, int yy, int destx, int desty) {
	unsigned char unit[unit_id][8];
	unit[unit_id][0] = side;
	unit[unit_id][1] = type;
	unit[unit_id][2] = health;
	unit[unit_id][3] = speed;
	unit[unit_id][4] = xx;
	unit[unit_id][5] = yy;
	unit[unit_id][6] = destx;
	unit[unit_id][7] = desty;
	create_sprite(unit_id,type,side,xx,yy);
	unit_id++;
}
void game_start() {
	SetSpritesTileTable(player_sprites);
	SetTileTable(terrain);
	DrawMap(0,0,map_normal);
	//just a placeholding content, this needs to initialize the game itself	after the player has pressed start..
	unsigned int xxx = 8;
	while(unts <= units_create) {
		create_unit(unit_vars[0],unit_vars[1],unit_vars[2],unit_vars[3],xxx,unit_vars[5],unit_vars[6],unit_vars[7]);
		unts+=1;
		xxx+=8; //increase sprite_x by 8
	}
}
yes i know the code's kinda messy, but i can'rt get it to work at all.. :S

i might be missing some variables here, but it's just to give you the basic idea of what i'm doing..

User avatar
nicksen782
Posts: 667
Joined: Wed Feb 01, 2012 8:23 pm
Location: Detroit, United States
Contact:

Re: efficient way to draw mode 3 sprites?

Post by nicksen782 » Thu Feb 06, 2014 10:58 pm

in create_sprite, you have map and movesprite with the first arguement being 0. that will always map and move with the same id. lets say your sprite is 4 tiles and you use 0 as the id. It would take sprites 0-3. In your case, its always the same sprites. Perhaps you should pass the sprite id into that function as well?

CunningFellow
Posts: 1174
Joined: Mon Feb 11, 2013 8:08 am
Location: Brisbane, Australia

Re: efficient way to draw mode 3 sprites?

Post by CunningFellow » Fri Feb 07, 2014 3:06 am

Not an efficient way to draw sprites.

but a more efficient way to manage objects.

Code: Select all

#define MaxUnits 30

#define InactiveUnit 0
#define PlayerUnit   1
#define ComputerUnit 2


typedef struct {
    unsigned char Side : 4;    // 1/2 a byte
    unsigned char Type : 4;    // 1/2 a byte
    Unsigned Char Health;      // 1 byte
    Unsigned Char Speed;       // 1 byte
    int X;                     // 2 bytes
    int Y;                     // 2 bytes
    int DestX;                 // 2 bytes
    int DestY;                 // 2 bytes
} UnitStruct;                  //          = Total 11 Bytes
The to use the struct

Code: Select all

unsinged char i;
UnitStruct Units[MaxUnits];

for(i=0;i<MaxUnits;i++) {

  Units[i].Side = InactiveUnit;

}
Defines and Variable names in structs are easier to remember.
Structs take up less space than a flat array.
Structs are easier for other people to read when you are asking for help in future.

That flat array of ints for your UNIT_VARS takes up 16 bytes. The struct above takes 11 bytes. Thats 30% saving of precious RAM.

User avatar
schepers_cp
Posts: 125
Joined: Tue Feb 04, 2014 9:48 pm
Location: netherlands
Contact:

Re: efficient way to draw mode 3 sprites?

Post by schepers_cp » Fri Feb 07, 2014 7:12 pm

@ nicksen782 i don't know what you mean with that sadly..
@ cunningfellow thanks for the script, even though it's not relative to my question about drawing sprites XD

User avatar
nicksen782
Posts: 667
Joined: Wed Feb 01, 2012 8:23 pm
Location: Detroit, United States
Contact:

Re: efficient way to draw mode 3 sprites?

Post by nicksen782 » Sat Feb 08, 2014 3:13 am

schepers_cp wrote:@ nicksen782 i don't know what you mean with that sadly..
@ cunningfellow thanks for the script, even though it's not relative to my question about drawing sprites XD
Your function always maps and moves sprite 0 (which would be sprites 0-3 for your 2x2 megasprite.) What I was saying was that instead of using 0 each time you could pass that number to your function.

Cunningfellow was suggesting a struct and that it would take a few less bytes of RAM. More importantly it organizes the variables used and puts them together in one place that makes your code easier to read. In fact, a struct is re-usable. Perhaps you can apply that same struct to multiple similar sprites. You could then access the values like player1.xpos or player2.xpos.
hi all, i wonder, i've got some graphics working atm in video mode 3, but how can i assign a unique sprite-id to them, so if i want to move them, only the given sprite moves?
also, i can't seem to get more than 4 sprites at the same time...
In the struct you could have a variable called spriteid. You're first 2x2 sprite would take up 0-3 (and start at 0) so you would assign 0 to it. Your second instance of the struct could have it's spriteid = 4. So, to draw your sprite, you would pass in player1.spriteid and then pass that in your function to map and movesprite. Assuming player 1 and 2 would have similar structures (variables).

Was this any help?

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest