Cursor Problem (SDL)

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Lancer
Posts: 2
Joined: Mon Dec 12, 2005 6:37 pm

Cursor Problem (SDL)

Post by Lancer »

A couple of days ago I decided to start learning how to use SDL. After a bit of searching, I found this topic. I went on ahead and started working on my own program to introduce myself, using the changes from the lesson2.cpp file that pspblizz provided.

I managed to get something compiled rather easily. When I ran it on my PSP, I was expecting something to go wrong, like getting one of the error messages or having my PSP explode, but what happened was completely unexpected. The screen was all black, except for an unmovable cursor in the upper left corner. I tinkered around with the code for a while, but all I managed to do was make the cursor blink when I tried different video modes.

I was completely clueless about what was going on (and still am), so I decided to compile the lesson2.cpp from the topic I mentioned above, and exactly the same thing occured.

Here is my code, in case that is the source of the problem:

Code: Select all

//////// Includes ////////
#include <pspkernel.h>
#include <pspdebug.h>

#include <stdlib.h>

#include <SDL.h>

//////// Definitions ////////
#define printf pspDebugScreenPrintf
#define PSP_SCREEN_WIDTH 480
#define PSP_SCREEN_HEIGHT 272

//////// Function Prototypes ////////
void DrawPixel&#40;SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B&#41;;
void Slock&#40;SDL_Surface *screen&#41;;
void Sulock&#40;SDL_Surface *screen&#41;;

//////// Main Function ////////
extern "C" int SDL_main&#40;int argc, char *argv&#91;&#93;&#41;
&#123;
    if&#40;SDL_Init&#40;SDL_INIT_VIDEO&#41; < 0&#41;
    &#123;
        printf&#40;"Unable to initiate video&#58;\n%s", SDL_GetError&#40;&#41;&#41;;
        sceKernelSleepThread&#40;&#41;;    
    &#125;
    atexit&#40;SDL_Quit&#41;;
    
    SDL_Surface *screen;
    screen = SDL_SetVideoMode&#40;480, 272, 32, SDL_SWSURFACE | SDL_FULLSCREEN&#41;;
    if&#40;screen == NULL&#41;
    &#123;
        printf&#40;"Unable to set 480x272x32 video mode&#58;\n%s", SDL_GetError&#40;&#41;&#41;;
        sceKernelSleepThread&#40;&#41;;
    &#125;
    
    while&#40;1&#41;
    &#123;
        Slock&#40;screen&#41;;
        for&#40;int ly=0; ly >= PSP_SCREEN_HEIGHT; ly++&#41;
        &#123;
            for&#40;int lx=0; lx >= PSP_SCREEN_WIDTH; lx++&#41;
            &#123;
                DrawPixel&#40;screen, lx, ly, rand&#40;&#41;%256, rand&#40;&#41;%256, rand&#40;&#41;%256&#41;;
            &#125;
        &#125;
        Sulock&#40;screen&#41;;
        SDL_Flip&#40;screen&#41;;
    &#125;
    sceKernelSleepThread&#40;&#41;;
    return 0;
&#125;

//////// Function Definitions ////////
void DrawPixel&#40;SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B&#41;
&#123;
    Uint32 color = SDL_MapRGB&#40;screen->format, R, G, B&#41;;
    switch &#40;screen->format->BytesPerPixel&#41;
    &#123;
        case 1&#58; // Assuming 8-bpp
        &#123;
            Uint8 *bufp;
            bufp = &#40;Uint8 *&#41;screen->pixels + y*screen->pitch + x;
            *bufp = color;
        &#125;
        break;
        case 2&#58; // Probably 15-bpp or 16-bpp
        &#123;
            Uint16 *bufp;
            bufp = &#40;Uint16 *&#41;screen->pixels + y*screen->pitch/2 + x;
            *bufp = color;
        &#125;
        break;
        case 3&#58; // Slow 24-bpp mode, usually not used
        &#123;
            Uint8 *bufp;
            bufp = &#40;Uint8 *&#41;screen->pixels + y*screen->pitch + x * 3;
            if&#40;SDL_BYTEORDER == SDL_LIL_ENDIAN&#41;
            &#123;
                bufp&#91;0&#93; = color;
                bufp&#91;1&#93; = color >> 8;
                bufp&#91;2&#93; = color >> 16;
            &#125;
            else
            &#123;
            bufp&#91;2&#93; = color;
            bufp&#91;1&#93; = color >> 8;
            bufp&#91;0&#93; = color >> 16;
            &#125;
        &#125;
        break;
        case 4&#58; // Probably 32-bpp
        &#123;
            Uint32 *bufp;
            bufp = &#40;Uint32 *&#41;screen->pixels + y*screen->pitch/4 + x;
            *bufp = color;
        &#125;
        break;
    &#125;
&#125;

void Slock&#40;SDL_Surface *screen&#41;
&#123;
    if &#40; SDL_MUSTLOCK&#40;screen&#41; &#41;
    &#123;
        if &#40; SDL_LockSurface&#40;screen&#41; < 0 &#41;
        &#123;
            return;
        &#125;
    &#125;
&#125;

void Sulock&#40;SDL_Surface *screen&#41;
&#123;
    if &#40; SDL_MUSTLOCK&#40;screen&#41; &#41;
    &#123;
        SDL_UnlockSurface&#40;screen&#41;;
    &#125;
&#125;
I'll also post the contents of the Makefile, just in case:

Code: Select all

TARGET = sdltest
OBJS = main.o

INCDIR =
CFLAGS = -G0 -Wall -O2
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

LIBDIR =
LDFLAGS = 
LIBS= -lpspgu

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = SDL Test

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
PSPBIN = $&#40;PSPSDK&#41;/../bin
CFLAGS += `$&#40;PSPBIN&#41;/sdl-config --cflags`
LIBS += `$&#40;PSPBIN&#41;/sdl-config --libs`
include $&#40;PSPSDK&#41;/lib/build.mak
If anyone needs any more information to figure out why the cursor is appearing, please ask.

Thanks in advance.
mrbrown
Site Admin
Posts: 1537
Joined: Sat Jan 17, 2004 11:24 am

Post by mrbrown »

Call SDL_ShowCursor(SDL_DISABLE) to disable the cursor. Visit http://www.libsdl.org/ for SDL API documentation.
Lancer
Posts: 2
Joined: Mon Dec 12, 2005 6:37 pm

Post by Lancer »

Looks like the drawpixel function is what was causing nothing to appear. Once I moved on to blitting images things started working out much better.

Thanks to mrbrown for the link.
rinco
Posts: 255
Joined: Fri Jan 21, 2005 2:12 pm
Location: Canberra, Australia

Post by rinco »

The DrawPixel function looks good and it is unlikely that using blits updated the video display. Are you using the most recent version of SDL from svn?

edit: this seems to fix it...

Code: Select all

        for&#40;int ly=0; ly < PSP_SCREEN_HEIGHT; ly++&#41;
        &#123;
            for&#40;int lx=0; lx < PSP_SCREEN_WIDTH; lx++&#41;
            &#123;
                DrawPixel&#40;screen, lx, ly, rand&#40;&#41;%256, rand&#40;&#41;%256, rand&#40;&#41;%256&#41;;
            &#125;
        &#125;

Post Reply