gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Is it me, or the fscking emulator?

#11014 - blasty - Tue Sep 23, 2003 2:04 pm

Hi,
Today I just started programming for the GBA (in C)
I tried to write a basical program which loads a sprite into video memory (Mode 4)
It compiles fine but when I run my rom with VisualBoyadvance it displays two lines of pixels that change constantly and not my cool sprite :(

spritedata has been generated using an utillitie from gbadev.org
my code is:
Code:

#include"gba_types.h"
#include"gba_mem.h"
#include"gba_const.h"
#include "woei.h"

#ifdef MULTIBOOT
  volatile const u8 __gba_multiboot; // crt0.o const for multiboot mode
#endif

#define VIDC_BASE       0x4000000
#define DISPCNT         *(u32*)(VIDC_BASE)

#define DISP_BG2        0x400

#define OBJ_MAP_2D      0x0
#define OBJ_MAP_1D      0x40

#define VRAM_BASE       0x6000000 // Hier start et Video RAM :)
#define BG_PAL          0x5000000 // BG Palette

// macro om DISPCNT mee te berekenen
#define DISP_MODE(n)    (n & 7)

u16* VidRam = (u16 *)VRAM_BASE; 
u16* Palette = (u16*)BG_PAL;

void plotpixel(int x, int y, unsigned short int c) {
        VidRam[y + 120 + x] = c; 
}

int AgbMain() { 
        int y, x, i;

        DISPCNT = DISP_MODE(4)|DISP_BG2;

        for (i = 0; i < 256; i++)
                Palette[i] = kleurtjes[i];


        for(y = 0; y < 160; y++) {
                for (x = 0; x < 120; x++) {
                        plotpixel(x,y,Data[y*120+x]);

                }
        }

        return 0;
}


woei.h holds my sprite and palette data.
Any suggestions?

#11015 - Datch - Tue Sep 23, 2003 2:38 pm

First, I think there's an error in your plotPixel function. It should be y * 120 instead of y + 120.
Code:

void plotpixel(int x, int y, unsigned short int c) {
        VidRam[y * 120 + x] = c; 
}

I'm not very experienced with mode 4, but I think you can't plot only one pixel without a bitshift trick. Here's what I would try.
Code:

        for(x = 0; x < ((160 * 120) / 2); x++)
                        plotpixel(x,0,(Data[x*2] << 8) + Data[(x*2)+1]);

It's not tested! Hope it can helps! :)
_________________
Visit SR-388 now and get a 40% discount!

#11016 - blasty - Tue Sep 23, 2003 2:53 pm

Thx mate!
The error was indeed in my plotpixel function, what a dumb fault :(

And about mode 4, you can directly write to videomem with it, no need for bitshifting and such :)

#11020 - niltsair - Tue Sep 23, 2003 3:58 pm

The bit shifting previously mentionned was because you can only write 16bits/32bits data to video memory, and not 8bits. Thus, it's not problem if using mode 3 or 5, but for mode 4, where you use 8bits palette entries, then you have to either write 2 pixels at a time, or use bitshifting to retrieve and then copy back the upper or lower part previsouly there.
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)