[Solved] SDL black/blank screen on testsprite.c

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

Moderators: cheriff, TyRaNiD

Post Reply
asphodeli
Posts: 20
Joined: Sun Jun 29, 2008 2:44 pm
Location: Singapore

[Solved] SDL black/blank screen on testsprite.c

Post by asphodeli »

Hi all,
Am having trouble running SDL test code from psplibraries rev 2663. testsprite.c compiles and runs fine (I can see the printf statements in pspsh), but the display on the PSP Slim is black/blank.

Code: Select all

/* Simple program:  Move N sprites around on the screen as fast as possible */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>

#include "SDL.h"

#define DEBUG_FLIP 1
#define NUM_SPRITES	100
#define MAX_SPEED 	1

#include <pspdisplay.h>
#include "common.h"
//PSP module info
PSP_MODULE_INFO&#40;"testsprite", 0, 1, 1&#41;;
//Cause 640k ought to be enough for everybody?
PSP_HEAP_SIZE_KB&#40;-640&#41;;
//Set thread attribute to user-land and enable VFPU
PSP_MAIN_THREAD_ATTR&#40;PSP_THREAD_ATTR_USER|PSP_THREAD_ATTR_VFPU&#41;;

SDL_Surface *sprite;
int numsprites;
SDL_Rect *sprite_rects;
SDL_Rect *positions;
SDL_Rect *velocities;
int sprites_visible;
Uint16 sprite_w, sprite_h;

int LoadSprite&#40;SDL_Surface *screen, char *file&#41;
&#123;
	SDL_Surface *temp;

	/* Load the sprite image */
	sprite = SDL_LoadBMP&#40;file&#41;;
	if &#40; sprite == NULL &#41; &#123;
		fprintf&#40;stderr, "Couldn't load %s&#58; %s", file, SDL_GetError&#40;&#41;&#41;;
		return&#40;-1&#41;;
	&#125;

	/* Set transparent pixel as the pixel at &#40;0,0&#41; */
	if &#40; sprite->format->palette &#41; &#123;
		SDL_SetColorKey&#40;sprite, &#40;SDL_SRCCOLORKEY|SDL_RLEACCEL&#41;,
						*&#40;Uint8 *&#41;sprite->pixels&#41;;
	&#125;

	/* Convert sprite to video format */
	temp = SDL_DisplayFormat&#40;sprite&#41;;
	SDL_FreeSurface&#40;sprite&#41;;
	if &#40; temp == NULL &#41; &#123;
		fprintf&#40;stderr, "Couldn't convert background&#58; %s\n",
							SDL_GetError&#40;&#41;&#41;;
		return&#40;-1&#41;;
	&#125;
	sprite = temp;

	/* We're ready to roll. &#58;&#41; */
	return&#40;0&#41;;
&#125;

void MoveSprites&#40;SDL_Surface *screen, Uint32 background&#41;
&#123;
	int i, nupdates;
	SDL_Rect area, *position, *velocity;

	nupdates = 0;
	/* Erase all the sprites if necessary */
	if &#40; sprites_visible &#41; &#123;
		SDL_FillRect&#40;screen, NULL, background&#41;;
	&#125;

	/* Move the sprite, bounce at the wall, and draw */
	for &#40; i=0; i<numsprites; ++i &#41; &#123;
		position = &positions&#91;i&#93;;
		velocity = &velocities&#91;i&#93;;
		position->x += velocity->x;
		if &#40; &#40;position->x < 0&#41; || &#40;position->x >= &#40;screen->w - sprite_w&#41;&#41; &#41; &#123;
			velocity->x = -velocity->x;
			position->x += velocity->x;
		&#125;
		position->y += velocity->y;
		if &#40; &#40;position->y < 0&#41; || &#40;position->y >= &#40;screen->h - sprite_w&#41;&#41; &#41; &#123;
			velocity->y = -velocity->y;
			position->y += velocity->y;
		&#125;

		/* Blit the sprite onto the screen */
		area = *position;
		SDL_BlitSurface&#40;sprite, NULL, screen, &area&#41;;
		sprite_rects&#91;nupdates++&#93; = area;
	&#125;

#if DEBUG_FLIP
    &#123;
	if &#40; &#40;screen->flags & SDL_DOUBLEBUF&#41; == SDL_DOUBLEBUF &#41; &#123;
            static int t = 0;

            Uint32 color = SDL_MapRGB &#40;screen->format, 255, 0, 0&#41;;
            SDL_Rect r;
            r.x = &#40;sin&#40;&#40;float&#41;t * 2 * 3.1459&#41; + 1.0&#41; / 2.0 * &#40;screen->w-20&#41;;
            r.y = 0;
            r.w = 20;
            r.h = screen->h;

            SDL_FillRect &#40;screen, &r, color&#41;;
            t+=2;
        &#125;
    &#125;
#endif

	/* Update the screen! */
	if &#40; &#40;screen->flags & SDL_DOUBLEBUF&#41; == SDL_DOUBLEBUF &#41; &#123;
	    sceDisplayWaitVblankStart&#40;&#41;;
		SDL_Flip&#40;screen&#41;;
	&#125; else &#123;
		SDL_UpdateRects&#40;screen, nupdates, sprite_rects&#41;;
	&#125;
	sprites_visible = 1;
&#125;

/* This is a way of telling whether or not to use hardware surfaces */
Uint32 FastestFlags&#40;Uint32 flags, int width, int height, int bpp&#41;
&#123;
	const SDL_VideoInfo *info;

	/* Hardware acceleration is only used in fullscreen mode */
	flags |= SDL_FULLSCREEN;

	/* Check for various video capabilities */
	info = SDL_GetVideoInfo&#40;&#41;;
	if &#40; info->blit_hw_CC && info->blit_fill &#41; &#123;
		/* We use accelerated colorkeying and color filling */
		flags |= SDL_HWSURFACE;
	&#125;
	/* If we have enough video memory, and will use accelerated
	   blits directly to it, then use page flipping.
	 */
	if &#40; &#40;flags & SDL_HWSURFACE&#41; == SDL_HWSURFACE &#41; &#123;
		/* Direct hardware blitting without double-buffering
		   causes really bad flickering.
		 */
		if &#40; info->video_mem*1024 > &#40;height*width*bpp/8&#41; &#41; &#123;
			flags |= SDL_DOUBLEBUF;
		&#125; else &#123;
			flags &= ~SDL_HWSURFACE;
		&#125;
	&#125;

	/* Return the flags */
	return&#40;flags&#41;;
&#125;

int main&#40;int argc, char *argv&#91;&#93;&#41;
&#123;
	setupPSP&#40;&#41;;
	SDL_Surface *screen;
	Uint8 *mem;
	int width, height;
	Uint8  video_bpp;
	Uint32 videoflags;
	Uint32 background;
	int    i, done;
	SDL_Event event;
	Uint32 then, now, frames;

	/* Initialize SDL */
	if &#40; SDL_Init&#40;SDL_INIT_VIDEO | SDL_INIT_JOYSTICK&#41; < 0 &#41; &#123;
		fprintf&#40;stderr, "Couldn't initialize SDL&#58; %s\n",SDL_GetError&#40;&#41;&#41;;
		//exit&#40;1&#41;;
	&#125;
	printf&#40;"SDL Initialized\n"&#41;;
	//atexit&#40;SDL_Quit&#41;;

	if &#40;SDL_NumJoysticks&#40;&#41;&#41; &#123;
		SDL_JoystickOpen&#40;0&#41;;
	&#125;
    printf&#40;"Joystick Opened\n"&#41;;

	numsprites = NUM_SPRITES;
	videoflags = SDL_SWSURFACE|SDL_ANYFORMAT|SDL_DOUBLEBUF;
	width = 480;
	height = 272;
	video_bpp = 8;

	/* Set video mode */
	screen = SDL_SetVideoMode&#40;width, height, video_bpp, videoflags&#41;;
	if &#40; ! screen &#41; &#123;
		fprintf&#40;stderr, "Couldn't set %dx%d video mode&#58; %s\n",
					width, height, SDL_GetError&#40;&#41;&#41;;
		//exit&#40;2&#41;;
	&#125;
    printf&#40;"Video Mode set\n"&#41;;

	/* Load the sprite */
	if &#40; LoadSprite&#40;screen, "./icon.bmp"&#41; < 0 &#41; &#123;
		fprintf&#40;stderr, "Unable to load sprite&#58; %s\n", SDL_GetError&#40;&#41;&#41;;
		//exit&#40;1&#41;;
	&#125;
    printf&#40;"Sprite loaded.\n"&#41;;

	/* Allocate memory for the sprite info */
	mem = &#40;Uint8 *&#41;malloc&#40;4*sizeof&#40;SDL_Rect&#41;*numsprites&#41;;
	if &#40; mem == NULL &#41; &#123;
		SDL_FreeSurface&#40;sprite&#41;;
		fprintf&#40;stderr, "Out of memory!\n"&#41;;
		//exit&#40;2&#41;;
	&#125;
	printf&#40;"Memory set\n"&#41;;

	sprite_rects = &#40;SDL_Rect *&#41;mem;
	positions = sprite_rects;
	sprite_rects += numsprites;
	velocities = sprite_rects;
	sprite_rects += numsprites;
	sprite_w = sprite->w;
	sprite_h = sprite->h;
	srand&#40;time&#40;NULL&#41;&#41;;
	for &#40; i=0; i<numsprites; ++i &#41; &#123;
		positions&#91;i&#93;.x = rand&#40;&#41;%&#40;screen->w - sprite_w&#41;;
		positions&#91;i&#93;.y = rand&#40;&#41;%&#40;screen->h - sprite_h&#41;;
		positions&#91;i&#93;.w = sprite->w;
		positions&#91;i&#93;.h = sprite->h;
		velocities&#91;i&#93;.x = 0;
		velocities&#91;i&#93;.y = 0;
		while &#40; ! velocities&#91;i&#93;.x && ! velocities&#91;i&#93;.y &#41; &#123;
			velocities&#91;i&#93;.x = &#40;rand&#40;&#41;%&#40;MAX_SPEED*2+1&#41;&#41;-MAX_SPEED;
			velocities&#91;i&#93;.y = &#40;rand&#40;&#41;%&#40;MAX_SPEED*2+1&#41;&#41;-MAX_SPEED;
		&#125;
	&#125;
	background = SDL_MapRGB&#40;screen->format, 0x00, 0x00, 0x00&#41;;

	/* Print out information about our surfaces */
	printf&#40;"Screen is at %d bits per pixel\n",screen->format->BitsPerPixel&#41;;
	if &#40; &#40;screen->flags & SDL_HWSURFACE&#41; == SDL_HWSURFACE &#41; &#123;
		printf&#40;"Screen is in video memory\n"&#41;;
	&#125; else &#123;
		printf&#40;"Screen is in system memory\n"&#41;;
	&#125;
	if &#40; &#40;screen->flags & SDL_DOUBLEBUF&#41; == SDL_DOUBLEBUF &#41; &#123;
		printf&#40;"Screen has double-buffering enabled\n"&#41;;
	&#125;
	if &#40; &#40;sprite->flags & SDL_HWSURFACE&#41; == SDL_HWSURFACE &#41; &#123;
		printf&#40;"Sprite is in video memory\n"&#41;;
	&#125; else &#123;
		printf&#40;"Sprite is in system memory\n"&#41;;
	&#125;
	/* Run a sample blit to trigger blit acceleration */
	&#123; SDL_Rect dst;
		dst.x = 0;
		dst.y = 0;
		dst.w = sprite->w;
		dst.h = sprite->h;
		SDL_BlitSurface&#40;sprite, NULL, screen, &dst&#41;;
		SDL_FillRect&#40;screen, &dst, background&#41;;
	&#125;
	if &#40; &#40;sprite->flags & SDL_HWACCEL&#41; == SDL_HWACCEL &#41; &#123;
		printf&#40;"Sprite blit uses hardware acceleration\n"&#41;;
	&#125;
	if &#40; &#40;sprite->flags & SDL_RLEACCEL&#41; == SDL_RLEACCEL &#41; &#123;
		printf&#40;"Sprite blit uses RLE acceleration\n"&#41;;
	&#125;

	/* Loop, blitting sprites and waiting for a keystroke */
	frames = 0;
	then = SDL_GetTicks&#40;&#41;;
	done = 0;
	sprites_visible = 0;
	while &#40; !done &#41; &#123;
		/* Check for events */
		++frames;
		while &#40; SDL_PollEvent&#40;&event&#41; &#41; &#123;
			switch &#40;event.type&#41; &#123;
				case SDL_MOUSEBUTTONDOWN&#58;
					SDL_WarpMouse&#40;screen->w/2, screen->h/2&#41;;
					break;
				case SDL_JOYBUTTONDOWN&#58;
				case SDL_KEYDOWN&#58;
					/* Any keypress quits the app... */
				case SDL_QUIT&#58;
					done = 1;
					break;
				default&#58;
					break;
			&#125;
		&#125;
		MoveSprites&#40;screen, background&#41;;
	&#125;
	SDL_FreeSurface&#40;sprite&#41;;
	free&#40;mem&#41;;

	/* Print out some timing information */
	now = SDL_GetTicks&#40;&#41;;
	if &#40; now > then &#41; &#123;
		printf&#40;"%2.2f frames per second\n",
					&#40;&#40;double&#41;frames*1000&#41;/&#40;now-then&#41;&#41;;
	&#125;
	SDL_Quit&#40;&#41;;
	return&#40;0&#41;;
&#125;
Makefile:

Code: Select all

PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;

TARGET = testsprite
OBJS = testsprite.o

INCDIR = -I ../../include/ /usr/local/pspdev/psp/include/SDL
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

PSPBIN = $&#40;PSPSDK&#41;/../bin
CFLAGS += $&#40;shell $&#40;PSPBIN&#41;/sdl-config --cflags | sed s/-Dmain=SDL_main//&#41;
LIBS += $&#40;shell $&#40;PSPBIN&#41;/sdl-config --libs | sed s/-lSDLmain//&#41;

LIBDIR = -L ../../lib/Linux LIBDIR
LIBS= -lSDL_image -lSDL_mixer -lSDL -lGLU -lGL -lpspgu -lstdc++ -lm -lc -lpsputility -lpspvfpu -lpsprtc -lpspaudio -lpsphprm -lpspirkeyb -lpsppower
#-lpspsystemctrl_kernel
LDFLAGS =

PSP_FW_VERSION = 390
BUILD_PRX=1

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Template

include $&#40;PSPSDK&#41;/lib/build.mak
Last edited by asphodeli on Sat Nov 08, 2008 11:43 am, edited 1 time in total.
asphodeli
Posts: 20
Joined: Sun Jun 29, 2008 2:44 pm
Location: Singapore

Post by asphodeli »

J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

asphodeli wrote:Possibly related to JF's post: http://forums.ps2dev.org/viewtopic.php?p=75692#75692
The bug talked about was in my TV version of SDL, not the repo version. Feel free to try the TV version if you want though, but I doubt that's the problem. More likely it's something with the colorkeying. I'm not sure the PSP version of SDL supports that.
asphodeli
Posts: 20
Joined: Sun Jun 29, 2008 2:44 pm
Location: Singapore

Post by asphodeli »

J.F. wrote:
asphodeli wrote:Possibly related to JF's post: http://forums.ps2dev.org/viewtopic.php?p=75692#75692
The bug talked about was in my TV version of SDL, not the repo version. Feel free to try the TV version if you want though, but I doubt that's the problem. More likely it's something with the colorkeying. I'm not sure the PSP version of SDL supports that.
Okay, problem solved. Just have to simply set video_bpp to 32, if not there will not be anything on the screen. ::)
Post Reply