Skip to content

Commit

Permalink
If no Video mode specified default to SDL_DC_DMA_VIDEO with SDL_HINT_…
Browse files Browse the repository at this point in the history
…VIDEO_DOUBLE_BUFFER enabled
  • Loading branch information
GPF committed Jan 9, 2025
1 parent a06f118 commit c0e02de
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 33 deletions.
6 changes: 5 additions & 1 deletion src/video/dreamcast/SDL_dreamcastframebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ int SDL_DREAMCAST_CreateWindowFramebuffer(_THIS, SDL_Window *window, Uint32 *for
h = SDL_powerof2(h);
} else {
// Default to the original bit depth from the display mode
target_bpp = SDL_BITSPERPIXEL(surface_format); // Maintain the original depth if not textured mode
target_bpp = SDL_BITSPERPIXEL(surface_format) ; // Maintain the original depth if not textured mode
if (target_bpp == 24) {
target_bpp = 32;
}
}
surface->pitch = w * (target_bpp / 8);

Expand Down Expand Up @@ -220,6 +223,7 @@ int SDL_DREAMCAST_CreateWindowFramebuffer(_THIS, SDL_Window *window, Uint32 *for
*format = surface_format;
*pixels = surface->pixels;
*pitch = surface->pitch;
SDL_Log("SDL_DREAMCAST_CreateWindowFramebuffer: %d x %d, %d bpp", w, h, target_bpp);
return 0;
}

Expand Down
66 changes: 43 additions & 23 deletions src/video/dreamcast/SDL_dreamcastvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@ int DREAMCAST_VideoInit(_THIS) {
SDL_SetError("Failed to add video display");
return -1;
}

if (video_mode_hint == NULL){
SDL_SetHint(SDL_HINT_DC_VIDEO_MODE, "SDL_DC_DMA_VIDEO"); // Default video mode
SDL_SetHint(SDL_HINT_VIDEO_DOUBLE_BUFFER, "1"); // Default double buffering
}
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "0");
SDL_Log("SDL2 Dreamcast video initialized: %dx%d, RGB565", width, height);
return 0;
}
Expand All @@ -252,7 +256,7 @@ void DREAMCAST_GetDisplayModes(_THIS, SDL_VideoDisplay *display) {
SDL_zero(mode);
mode.w = supported_modes[i][0];
mode.h = supported_modes[i][1];
mode.format = SDL_PIXELFORMAT_RGB565; // Change as necessary
mode.format = SDL_PIXELFORMAT_ARGB1555; // Change as necessary
mode.refresh_rate = refresh_rate;
SDL_AddDisplayMode(display, &mode);
}
Expand All @@ -264,44 +268,56 @@ int DREAMCAST_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *
int pixel_mode;
int disp_mode = -1;

// Determine the appropriate display mode
if (__sdl_dc_is_60hz) {
#ifdef SDL_VIDEO_OPENGL
disp_mode = DM_640x480;
#ifndef SDL_VIDEO_OPENGL
// Enforce OpenGL requirement for 640x480 if SDL_VIDEO_OPENGL is disabled
if (SDL_GetCurrentVideoDriver() && strcmp(SDL_GetCurrentVideoDriver(), "opengl") == 0) {
mode->w = 640;
mode->h = 480;
}
#else
#endif

// Detect cable and region
if (!vid_check_cable()) {
__sdl_dc_is_60hz = 1;
} else if (flashrom_get_region() != FLASHROM_REGION_US && !sdl_dc_ask_60hz()) {
__sdl_dc_is_60hz = 0;
} else {
__sdl_dc_is_60hz = 1;
}

// Determine display mode based on resolution and refresh rate
if (__sdl_dc_is_60hz) {
if (mode->w == 320 && mode->h == 240) {
disp_mode = DM_320x240;
} else if (mode->w == 640 && mode->h == 480) {
disp_mode = DM_640x480;
} else if (mode->w == 768 && mode->h == 480) {
disp_mode = DM_768x480;
}
}
#endif

else {
#ifdef SDL_VIDEO_OPENGL
disp_mode = DM_640x480_PAL_IL;
}
#else
} else {
if (mode->w == 320 && mode->h == 240) {
disp_mode = DM_320x240_PAL;
} else if (mode->w == 640 && mode->h == 480) {
disp_mode = DM_640x480_PAL_IL;
} else if (mode->w == 768 && mode->h == 576) {
disp_mode = DM_768x576_PAL_IL;
}
}
#endif


if (disp_mode < 0) {
SDL_SetError("Unsupported display mode");
SDL_SetError("Unsupported display mode: %dx%d", mode->w, mode->h);
return -1;
}

// Determine pixel mode
// Ensure mode->format is set
if (mode->format == 0) {
mode->format = SDL_PIXELFORMAT_RGB565; // Default pixel format
}

// Map SDL pixel format to Dreamcast pixel mode
switch (mode->format) {
case SDL_PIXELFORMAT_ARGB1555:
pixel_mode = PM_RGB555;
pixel_mode = PM_RGB0888;
break;
case SDL_PIXELFORMAT_RGB565:
pixel_mode = PM_RGB565;
Expand All @@ -310,13 +326,17 @@ int DREAMCAST_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *
pixel_mode = PM_RGB888;
break;
default:
SDL_SetError("Unsupported pixel format");
SDL_SetError("Unsupported pixel format: %s", SDL_GetPixelFormatName(mode->format));
return -1;
}

// Set the video mode
// Set video mode in hardware
vid_set_mode(disp_mode, pixel_mode);
SDL_Log("Display mode set to %dx%d @ %dHz", mode->w, mode->h, mode->refresh_rate);

SDL_Log("Display mode set: %dx%d @ %dHz, format %s",
mode->w, mode->h, __sdl_dc_is_60hz ? 60 : 50,
SDL_GetPixelFormatName(mode->format));

return 0;
}

Expand Down
50 changes: 41 additions & 9 deletions test/dreamcast/test/sdlanimation/sdlanim.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ int main(int argc, char* argv[])
int currentDirection = DIR_RIGHT;
int animationFlip = 0;
SDL_Rect spritePosition;

// SDL_SetHint(SDL_HINT_VIDEO_DOUBLE_BUFFER, "1");
// SDL_SetHint(SDL_HINT_DC_VIDEO_MODE, "SDL_DC_TEXTURED_VIDEO");
// SDL_SetHint(SDL_HINT_DC_VIDEO_MODE, "SDL_DC_DIRECT_VIDEO");
/* initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
SDL_Log("Failed to initialize SDL: %s", SDL_GetError());
Expand Down Expand Up @@ -91,9 +93,9 @@ int main(int argc, char* argv[])
// Set SDL hint for the renderer
// SDL_SetHint(SDL_HINT_DC_VIDEO_MODE, "SDL_DC_TEXTURED_VIDEO");
// SDL_SetHint(SDL_HINT_DC_VIDEO_MODE, "SDL_DC_DMA_VIDEO"); // Set for DMA mode
// SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "software");
// renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE| SDL_RENDERER_PRESENTVSYNC);
renderer = SDL_CreateRenderer(window, -1, 0);
// SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "opengl");
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
// renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) {
SDL_Log("Failed to create renderer: %s", SDL_GetError());
SDL_DestroyWindow(window);
Expand All @@ -110,12 +112,38 @@ int main(int argc, char* argv[])
SDL_Quit();
return 1;
}
// SDL_SetColorKey(temp, SDL_TRUE, SDL_MapRGB(temp->format, 255, 0, 255));
SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *)temp->pixels);

// spriteTexture = SDL_CreateTextureFromSurface(renderer, temp);
SDL_Surface* converted_surface1 = SDL_ConvertSurfaceFormat(temp, SDL_PIXELFORMAT_ARGB8888, 0);
// // Uint32 magenta = SDL_MapRGBA(converted_surface1->format, 255, 0, 255,0);
Uint32 *pixels = (Uint32 *)converted_surface1->pixels;
Uint32 pixel0Color = pixels[0] & 0x00FFFFFF; // Extract only RGB from pixel 0
// Loop through each pixel to set alpha based on pixel 0's color
// for (int y = 0; y < converted_surface1->h; ++y) {
// for (int x = 0; x < converted_surface1->w; ++x) {
// int pixelIndex = y * converted_surface1->w + x;
// if ((pixels[pixelIndex] & 0x00FFFFFF) == pixel0Color) {
// // Set alpha to 0 for transparency
// // printf("setting transparency\n");
// pixels[pixelIndex] = (pixels[pixelIndex] & 0x00FFFFFF) | 0xFF000000;
// } else {
// // Set alpha to 255 for opacity
// pixels[pixelIndex] = (pixels[pixelIndex] & 0x00FFFFFF) | 0xFF000000;
// }
// }
// }
// Uint32 checkColor = pixel0Color;
// Uint8 r, g, b, a;
// SDL_GetRGBA(checkColor, converted_surface1->format, &r, &g, &b, &a);
// SDL_Log("Pixel 0 after setting to magenta: (R, G, B, A): %d, %d, %d, %d", r, g, b, a);
spriteTexture = SDL_CreateTextureFromSurface(renderer, converted_surface1);
SDL_FreeSurface(temp);


SDL_FreeSurface(temp);
Uint32 format;
SDL_QueryTexture(spriteTexture, &format, NULL, NULL, NULL);
SDL_Log("Sprite texture format: %s", SDL_GetPixelFormatName(format));
if (!spriteTexture) {
SDL_Log("Failed to create sprite texture: %s", SDL_GetError());
SDL_DestroyRenderer(renderer);
Expand All @@ -134,8 +162,9 @@ int main(int argc, char* argv[])
SDL_Quit();
return 1;
}
// SDL_SetColorKey(temp, SDL_TRUE, SDL_MapRGB(temp->format, 255, 0, 255));
// grassTexture = SDL_CreateTextureFromSurface(renderer, temp);
SDL_Surface* converted_surface = SDL_ConvertSurfaceFormat(temp, SDL_PIXELFORMAT_ARGB1555, 0);
SDL_Surface* converted_surface = SDL_ConvertSurfaceFormat(temp, SDL_PIXELFORMAT_ARGB8888, 0);
grassTexture = SDL_CreateTextureFromSurface(renderer, converted_surface);
SDL_FreeSurface(temp);

Expand Down Expand Up @@ -200,15 +229,18 @@ int main(int argc, char* argv[])

/* clear the screen */
SDL_RenderClear(renderer);

// SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
// SDL_SetTextureBlendMode(grassTexture, SDL_BLENDMODE_NONE);
/* draw the background */
for (int x = 0; x < SCREEN_WIDTH / GRASS_SIZE; x++) {
for (int y = 0; y < SCREEN_HEIGHT / GRASS_SIZE; y++) {
SDL_Rect position = {x * GRASS_SIZE, y * GRASS_SIZE, GRASS_SIZE, GRASS_SIZE};
SDL_RenderCopy(renderer, grassTexture, NULL, &position);
}
}
// SDL_SetTextureBlendMode(spriteTexture, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE );
SDL_SetTextureBlendMode(spriteTexture, SDL_BLENDMODE_BLEND);
// SDL_SetTextureAlphaMod(spriteTexture, pixel0Color);
/* Draw the selected image of the sprite at the right position */
{
SDL_Rect spriteImage;
Expand Down

0 comments on commit c0e02de

Please sign in to comment.