Several newb questions

What is a Uzebox? How can I get one? Check here!
User avatar
danboid
Posts: 88
Joined: Sun Jun 14, 2020 12:14 am

Re: Several newb questions

Post by danboid » Wed Jul 08, 2020 3:55 pm

Thanks for that very comprehensive response Artcfox!

So it turns out what I wrote was a fluke and wasn't correct at all then! :D

I have made this effort at correcting my code but it clearly has some work to be done yet as I don't see anything being drawn to the screen.

Yes, I'm just trying to move 1x 8x8 sprite around the screen. In one of you snippets you mention `sprites[3]` but I'm not sure what that is referring to, how your are intending that be instantiated.

Here is my latest attempt that builds but doesn't show anything. I used mysprites for the variable name in tileset.inc.

I think a working version of this would be make a good demo to include in the repo as a mid-point example between hello world and pong.

Code: Select all

#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <stdlib.h>
#include <string.h>
#include <avr/pgmspace.h>
#include <uzebox.h>

#include "data/tileset.inc"

int btnPrev = 0;     // Previous button
int btnHeld = 0;     // buttons that are held right now
int btnPressed = 0;  // buttons that were pressed this frame
int btnReleased = 0; // buttons that were released this frame 

struct PlayerStruct
 {
   u8 x;
   u8 y;
   u8 tileIndex;
   u8 flags;		
 };	

struct PlayerStruct Player;

int main()
{
	ClearVram();
	SetSpritesTileBank(0, mysprites);
	Player.x = 100;
	Player.y = 100;
	Player.tileIndex = 1;
	SetSpriteVisibility(true);
	
	while(1){
        WaitVsync(1);
    
        btnHeld = ReadJoypad(0);
        btnPressed = btnHeld & (btnHeld ^ btnPrev);
        btnReleased = btnPrev & (btnHeld ^ btnPrev);
    
        if(btnHeld & BTN_RIGHT){
            Player.x+=3;
        }
        if(btnHeld & BTN_LEFT){
            Player.x-=3;
        }
        if(btnHeld & BTN_UP){
            Player.y-=3;
        }
        if(btnHeld & BTN_DOWN){
            Player.y+=3;
        }
        btnPrev = btnHeld;
    }
}

User avatar
danboid
Posts: 88
Joined: Sun Jun 14, 2020 12:14 am

Re: Several newb questions

Post by danboid » Fri Jul 10, 2020 10:08 am

The UB docs would benefit greatly from having complete, runnable code examples instead of just giving code snippets.

You can't beat runnable/buildable examples for clarity.

feluga
Posts: 5
Joined: Sat Jun 27, 2020 12:44 am

Re: Several newb questions

Post by feluga » Fri Jul 10, 2020 5:20 pm

danboid wrote:
Fri Jul 10, 2020 10:08 am
The UB docs would benefit greatly from having complete, runnable code examples instead of just giving code snippets.
Actually there is a GitHub for UB which is a great source of complete runnable coded games and examples.
Demos and games can be found at https://github.com/Uzebox/uzebox/tree/master/demos

User avatar
Artcfox
Posts: 1011
Joined: Thu Jun 04, 2015 5:35 pm
Contact:

Re: Several newb questions

Post by Artcfox » Sat Jul 11, 2020 7:45 am

I encapsulated all of the sprite stuff in a render function in my game, you should read that function because it will show you what you are missing: https://github.com/artcfox/bugz/blob/ma ... ity.c#L860

The variable:

Code: Select all

sprites
exists within the kernel in a mode 3 Uzebox game, so pay close attention to any line that references that variable, because you will need to set it's tileIndex, flags, x, and y.

That function chooses a tileIndex and flags based on the internal state of the entity object, and then it sets the actual sprite's tileIndex, flags, x, and y.

If you're starting out, just try something with sprites[0], set it's tileIndex, x, and y. Also, read some of the example code, there is a ton of code out there.

Edit: You can also see how I animate my sprites, by using a frameCounter to select a different tileIndex every N frames.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests