SDL Dreamcast Question

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 576
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Fri Jun 18, 2010 9:29 pm
Has thanked: 0
Been thanked: 5 times

SDL Dreamcast Question

Post by PH3NOM »

Hello everyone!
I am trying to do my own port of libmpeg2 to Dreamcast. (For now, if successful more things will come...)

I am trying to use SDL for the video driver.

For this project, everything compiles fine, but after the video mode select screen
"Press 'Y' for 60Hz"
the app resets on Dreamcast.

Since im relatively new to Dreamcast KOS, I need to find out is if this is due to an incompatible video driver.

Can anyone here tell, by looking at the code, if this driver with work with Dreamcast SDL?
( Its libvo\video_out_sdl.c, I just modified it to use fast doublebuff instead of sdl_resizeable )

Code: Select all

#include "config.h"

#ifdef LIBVO_SDL

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include <SDL_dreamcast.h>
#include <inttypes.h>


#include "video_out.h"
#include "vo_internal.h"

typedef struct {
    vo_instance_t vo;
    int width;
    int height;
    SDL_Surface * surface;
    Uint32 sdlflags;
    Uint8 bpp;
} sdl_instance_t;

static void sdl_setup_fbuf (vo_instance_t * _instance,
			    uint8_t ** buf, void ** id)
{
    sdl_instance_t * instance = (sdl_instance_t *) _instance;
    SDL_Overlay * overlay;

    *id = overlay = SDL_CreateYUVOverlay (instance->width, instance->height,
					  SDL_YV12_OVERLAY, instance->surface);
    buf[0] = overlay->pixels[0];
    buf[1] = overlay->pixels[2];
    buf[2] = overlay->pixels[1];
    if (((long)buf[0] & 15) || ((long)buf[1] & 15) || ((long)buf[2] & 15)) {
	fprintf (stderr, "Unaligned buffers. Anyone know how to fix this ?\n");
	exit (1);
    }
}

static void sdl_start_fbuf (vo_instance_t * instance,
			    uint8_t * const * buf, void * id)
{
    SDL_LockYUVOverlay ((SDL_Overlay *) id);
}

static void sdl_draw_frame (vo_instance_t * _instance,
			    uint8_t * const * buf, void * id)
{
    sdl_instance_t * instance = (sdl_instance_t *) _instance;
    SDL_Overlay * overlay = (SDL_Overlay *) id;
    SDL_Event event;

    while (SDL_PollEvent (&event))
	if (event.type == SDL_VIDEORESIZE)
	    instance->surface =
		SDL_SetVideoMode (event.resize.w, event.resize.h,
				  instance->bpp, instance->sdlflags);
    SDL_DisplayYUVOverlay (overlay, &(instance->surface->clip_rect));
}

static void sdl_discard (vo_instance_t * _instance,
			 uint8_t * const * buf, void * id)
{
    SDL_UnlockYUVOverlay ((SDL_Overlay *) id);
}

#if 0
static void sdl_close (vo_instance_t * _instance)
{
    sdl_instance_t * instance;
    int i;

    instance = (sdl_instance_t *) _instance;
    for (i = 0; i < 3; i++)
	SDL_FreeYUVOverlay (instance->frame[i].overlay);
    SDL_FreeSurface (instance->surface);
    SDL_QuitSubSystem (SDL_INIT_VIDEO);
}
#endif

static int sdl_setup (vo_instance_t * _instance, unsigned int width,
		      unsigned int height, unsigned int chroma_width,
		      unsigned int chroma_height, vo_setup_result_t * result)
{
    sdl_instance_t * instance;

    instance = (sdl_instance_t *) _instance;

    instance->width = width;
    instance->height = height;
    instance->surface = SDL_SetVideoMode (width, height, instance->bpp,
					  instance->sdlflags);		  
  
    if (! (instance->surface)) {
	fprintf (stderr, "sdl could not set the desired video mode\n");
	return 1;
    }

    result->convert = NULL;
    return 0;
}

vo_instance_t * vo_sdl_open (void)
{
    sdl_instance_t * instance;
    const SDL_VideoInfo * vidInfo;

    instance = (sdl_instance_t *) malloc (sizeof (sdl_instance_t));
    if (instance == NULL)
	return NULL;

    instance->vo.setup = sdl_setup;
    instance->vo.setup_fbuf = sdl_setup_fbuf;
    instance->vo.set_fbuf = NULL;
    instance->vo.start_fbuf = sdl_start_fbuf;
    instance->vo.discard = sdl_discard;
    instance->vo.draw = sdl_draw_frame;
    instance->vo.close = NULL; /* sdl_close; */
    instance->sdlflags = SDL_FULLSCREEN|SDL_DOUBLEBUF|SDL_HWSURFACE;

    //putenv((char *)"SDL_VIDEO_YUV_HWACCEL=1");
    //putenv((char *)"SDL_VIDEO_X11_NODIRECTCOLOR=1");

    if (SDL_Init (SDL_INIT_VIDEO)) {
	fprintf (stderr, "sdl video initialization failed.\n");
	return NULL;
    }

    vidInfo = SDL_GetVideoInfo ();
    if (!SDL_ListModes (vidInfo->vfmt, SDL_FULLSCREEN|SDL_DOUBLEBUF|SDL_HWSURFACE)) {
	instance->sdlflags = SDL_FULLSCREEN|SDL_DOUBLEBUF|SDL_HWSURFACE;
	//if (!SDL_ListModes (vidInfo->vfmt, SDL_RESIZABLE)) {
	//    fprintf (stderr, "sdl couldn't get any acceptable video mode\n");
	 //   return NULL;
	//}
    }
    instance->bpp = vidInfo->vfmt->BitsPerPixel;
    if (instance->bpp < 16) {
	fprintf(stderr, "sdl has to emulate a 16 bit surfaces, "
		"that will slow things down.\n");
	instance->bpp = 16;
    }

    return (vo_instance_t *) instance;
}
#endif
Any replies appreciated! Thanks.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: SDL Dreamcast Question

Post by BlueCrab »

I'd really recommend that you don't use SDL for something like that on the Dreamcast. There are much better ways of interacting with things than SDL. SDL on the Dreamcast is extremely buggy and really quite slow.
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 576
Joined: Fri Jun 18, 2010 9:29 pm
Has thanked: 0
Been thanked: 5 times

Re: SDL Dreamcast Question

Post by PH3NOM »

Thanks BlueCrab.
Too bad, I was under the impression Dreamcast SDL was quite solid...

At any rate, I dont want to spend time writing a custom video driver If there is a major issue elsewhere in the code..

So, do you see anything in the code posted, that will be particularly incompatible?
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5658
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: SDL Dreamcast Question

Post by BlueCrab »

In all honesty, I'd halfway be willing to blame it on a SDL bug. I haven't worked with SDL in ages, but it hasn't changed all that much either. Granted, I don't see anything in particular in your code there that would cause any issues (then again, I dunno how well the YUV stuff works in SDL, and its assuredly done in software on the Dreamcast anyway).

That said, its pretty easy to work with KallistiOS directly to get a simple framebuffer rendering scheme running, and its not all that hard to move from that to using the PVR to do any rendering. There are ways of making the PVR do YUV conversion, however I don't know that anyone has ever used it for homebrew purposes (I know I haven't seen any code use the YUV stuff), so I don't know of anyone who could help with that kind of stuff...
OneThirty8
Damn Dirty Ape
Damn Dirty Ape
Posts: 5031
Joined: Thu Nov 07, 2002 11:11 pm
Location: Saugerties, NY
Has thanked: 0
Been thanked: 0

Re: SDL Dreamcast Question

Post by OneThirty8 »

BlueCrab wrote:In all honesty, I'd halfway be willing to blame it on a SDL bug. I haven't worked with SDL in ages, but it hasn't changed all that much either. Granted, I don't see anything in particular in your code there that would cause any issues (then again, I dunno how well the YUV stuff works in SDL, and its assuredly done in software on the Dreamcast anyway).

That said, its pretty easy to work with KallistiOS directly to get a simple framebuffer rendering scheme running, and its not all that hard to move from that to using the PVR to do any rendering. There are ways of making the PVR do YUV conversion, however I don't know that anyone has ever used it for homebrew purposes (I know I haven't seen any code use the YUV stuff), so I don't know of anyone who could help with that kind of stuff...
Libmpeg2 will output uyvy, and you can send that directly to the PVR. It's the same as YUV422. It worked perfectly in VC/DC. Code is available at http://code.google.com/p/vcdc/downloads/list although much of it is very difficult to read. Also, if you can find lvfdc, that was based on libmpeg2 as well, albeit an older version.
User avatar
PH3NOM
DC Developer
DC Developer
Posts: 576
Joined: Fri Jun 18, 2010 9:29 pm
Has thanked: 0
Been thanked: 5 times

Re: SDL Dreamcast Question

Post by PH3NOM »

Thanks OneThirty8 I know you have the experience with this code.

It sounds like I will need to learn a bit more about working with the pvr hardware to make this work with decent speeds then. I will take a closer look at your video_out_dc.c.

I compiled the vcdc source you linked using dev iso r4 and r3.
Running the app on dc, it will draw a frame then freeze.
Can you confirm the source package you uploaded is working?

Thanks again.
Post Reply