src/video/bwindow/SDL_bmodes.cc
author Nathan Heisey <nathanheisey@gmail.com>
Wed, 20 Jul 2011 19:25:38 +0000
changeset 5921 4cec9ba57dfb
parent 5918 2063df37190b
child 5922 efd1498efdff
permissions -rw-r--r--
Framebuffer support in progress.
nathanheisey@5918
     1
/*
nathanheisey@5918
     2
  Simple DirectMedia Layer
nathanheisey@5918
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
nathanheisey@5918
     4
nathanheisey@5918
     5
  This software is provided 'as-is', without any express or implied
nathanheisey@5918
     6
  warranty.  In no event will the authors be held liable for any damages
nathanheisey@5918
     7
  arising from the use of this software.
nathanheisey@5918
     8
nathanheisey@5918
     9
  Permission is granted to anyone to use this software for any purpose,
nathanheisey@5918
    10
  including commercial applications, and to alter it and redistribute it
nathanheisey@5918
    11
  freely, subject to the following restrictions:
nathanheisey@5918
    12
nathanheisey@5918
    13
  1. The origin of this software must not be misrepresented; you must not
nathanheisey@5918
    14
     claim that you wrote the original software. If you use this software
nathanheisey@5918
    15
     in a product, an acknowledgment in the product documentation would be
nathanheisey@5918
    16
     appreciated but is not required.
nathanheisey@5918
    17
  2. Altered source versions must be plainly marked as such, and must not be
nathanheisey@5918
    18
     misrepresented as being the original software.
nathanheisey@5918
    19
  3. This notice may not be removed or altered from any source distribution.
nathanheisey@5918
    20
*/
nathanheisey@5918
    21
nathanheisey@5921
    22
nathanheisey@5921
    23
nathanheisey@5921
    24
#include <AppKit.h>
nathanheisey@5921
    25
#include <InterfaceKit.h>
nathanheisey@5921
    26
#include "SDL_bmodes.h"
nathanheisey@5921
    27
nathanheisey@5921
    28
#include "../../main/beos/SDL_BApp.h"
nathanheisey@5921
    29
nathanheisey@5921
    30
#ifdef __cplusplus
nathanheisey@5921
    31
extern "C" {
nathanheisey@5921
    32
#endif
nathanheisey@5921
    33
nathanheisey@5921
    34
nathanheisey@5921
    35
static inline SDL_BApp *_GetBeApp() {
nathanheisey@5921
    36
	return ((SDL_BApp*)be_app);
nathanheisey@5921
    37
}
nathanheisey@5921
    38
nathanheisey@5921
    39
nathanheisey@5921
    40
/* Copied from haiku/trunk/src/preferences/screen/ScreenMode.cpp */
nathanheisey@5921
    41
static float get_refresh_rate(display_mode &mode) {
nathanheisey@5921
    42
	return rint(10 * float(mode.timing.pixel_clock * 1000)
nathanheisey@5921
    43
		/ float(mode.timing.h_total * mode.timing.v_total)) / 10.0;
nathanheisey@5921
    44
}
nathanheisey@5921
    45
nathanheisey@5921
    46
static inline int ColorSpaceToBitsPerPixel(uint32 colorspace)
nathanheisey@5921
    47
{
nathanheisey@5921
    48
	int bitsperpixel;
nathanheisey@5921
    49
nathanheisey@5921
    50
	bitsperpixel = 0;
nathanheisey@5921
    51
	switch (colorspace) {
nathanheisey@5921
    52
	    case B_CMAP8:
nathanheisey@5921
    53
		bitsperpixel = 8;
nathanheisey@5921
    54
		break;
nathanheisey@5921
    55
	    case B_RGB15:
nathanheisey@5921
    56
	    case B_RGBA15:
nathanheisey@5921
    57
	    case B_RGB15_BIG:
nathanheisey@5921
    58
	    case B_RGBA15_BIG:
nathanheisey@5921
    59
		bitsperpixel = 15;
nathanheisey@5921
    60
		break;
nathanheisey@5921
    61
	    case B_RGB16:
nathanheisey@5921
    62
	    case B_RGB16_BIG:
nathanheisey@5921
    63
		bitsperpixel = 16;
nathanheisey@5921
    64
		break;
nathanheisey@5921
    65
	    case B_RGB32:
nathanheisey@5921
    66
	    case B_RGBA32:
nathanheisey@5921
    67
	    case B_RGB32_BIG:
nathanheisey@5921
    68
	    case B_RGBA32_BIG:
nathanheisey@5921
    69
		bitsperpixel = 32;
nathanheisey@5921
    70
		break;
nathanheisey@5921
    71
	    default:
nathanheisey@5921
    72
		break;
nathanheisey@5921
    73
	}
nathanheisey@5921
    74
	return(bitsperpixel);
nathanheisey@5921
    75
}
nathanheisey@5921
    76
nathanheisey@5921
    77
static inline int32 BppToSDLPxFormat(int32 bpp) {
nathanheisey@5921
    78
	/* Translation taken from SDL_windowsmodes.c */
nathanheisey@5921
    79
	switch (bpp) {
nathanheisey@5921
    80
	case 32:
nathanheisey@5921
    81
		return SDL_PIXELFORMAT_RGB888;
nathanheisey@5921
    82
		break;
nathanheisey@5921
    83
	case 24:	/* May not be supported by Haiku */
nathanheisey@5921
    84
		return SDL_PIXELFORMAT_RGB24;
nathanheisey@5921
    85
		break;
nathanheisey@5921
    86
	case 16:
nathanheisey@5921
    87
		return SDL_PIXELFORMAT_RGB565;
nathanheisey@5921
    88
		break;
nathanheisey@5921
    89
	case 15:
nathanheisey@5921
    90
		return SDL_PIXELFORMAT_RGB555;
nathanheisey@5921
    91
		break;
nathanheisey@5921
    92
	case 8:
nathanheisey@5921
    93
		return SDL_PIXELFORMAT_INDEX8;
nathanheisey@5921
    94
		break;
nathanheisey@5921
    95
	case 4:		/* May not be supported by Haiku */
nathanheisey@5921
    96
		return SDL_PIXELFORMAT_INDEX4LSB;
nathanheisey@5921
    97
		break;
nathanheisey@5921
    98
	}
nathanheisey@5921
    99
}
nathanheisey@5921
   100
nathanheisey@5921
   101
static inline void BE_BDisplayModeToSdlDisplayMode(display_mode *bmode,
nathanheisey@5921
   102
		SDL_DisplayMode *mode) {
nathanheisey@5921
   103
	mode->w = bmode->virtual_width;
nathanheisey@5921
   104
	mode->h = bmode->virtual_height;
nathanheisey@5921
   105
	mode->refresh_rate = (int)get_refresh_rate(*bmode);
nathanheisey@5921
   106
	mode->driverdata = bmode;	/* This makes setting display
nathanheisey@5921
   107
									   modes easier */
nathanheisey@5921
   108
nathanheisey@5921
   109
	/* Set the format */
nathanheisey@5921
   110
	int32 bpp = ColorSpaceToBitsPerPixel(bmode->space);
nathanheisey@5921
   111
	mode->format = BppToSDLPxFormat(bpp);
nathanheisey@5921
   112
}
nathanheisey@5921
   113
nathanheisey@5921
   114
/* Later, there may be more than one monitor available */
nathanheisey@5921
   115
void BE_AddDisplay(BScreen *screen) {
nathanheisey@5921
   116
	SDL_VideoDisplay display;
nathanheisey@5921
   117
	SDL_DisplayMode mode;
nathanheisey@5921
   118
	display_mode bmode;
nathanheisey@5921
   119
	screen->GetMode(&bmode);
nathanheisey@5921
   120
nathanheisey@5921
   121
	BE_BDisplayModeToSdlDisplayMode(&bmode, &mode);
nathanheisey@5921
   122
	
nathanheisey@5921
   123
	SDL_zero(display);
nathanheisey@5921
   124
	display.desktop_mode = mode;
nathanheisey@5921
   125
	display.current_mode = mode;
nathanheisey@5921
   126
	SDL_AddVideoDisplay(&display);
nathanheisey@5921
   127
}
nathanheisey@5918
   128
nathanheisey@5918
   129
int BE_InitModes(_THIS) {
nathanheisey@5921
   130
	printf("Init Modes\n");
nathanheisey@5921
   131
	BScreen screen;
nathanheisey@5921
   132
	
nathanheisey@5921
   133
	/* Save the current display mode */
nathanheisey@5921
   134
	display_mode *prevMode;
nathanheisey@5921
   135
	screen.GetMode(prevMode);
nathanheisey@5921
   136
	_GetBeApp()->SetPrevMode(prevMode);
nathanheisey@5918
   137
nathanheisey@5921
   138
	/* Only one possible video display right now */
nathanheisey@5921
   139
	BE_AddDisplay(&screen);
nathanheisey@5918
   140
}
nathanheisey@5918
   141
nathanheisey@5918
   142
int BE_QuitModes(_THIS) {
nathanheisey@5921
   143
	/* Restore the previous video mode */
nathanheisey@5921
   144
	printf("Quit Modes\n");
nathanheisey@5921
   145
	
nathanheisey@5921
   146
	BScreen screen;
nathanheisey@5921
   147
	display_mode *savedMode = _GetBeApp()->GetPrevMode();
nathanheisey@5921
   148
	screen.SetMode(savedMode);
nathanheisey@5921
   149
	return 0;
nathanheisey@5921
   150
}
nathanheisey@5921
   151
nathanheisey@5921
   152
nathanheisey@5921
   153
int BE_GetDisplayBounds(_THIS, SDL_VideoDisplay *display, SDL_Rect *rect) {
nathanheisey@5921
   154
	BScreen bscreen;
nathanheisey@5921
   155
	BRect rc = bscreen.Frame();
nathanheisey@5921
   156
	rect->x = (int)rc.left;
nathanheisey@5921
   157
	rect->y = (int)rc.top;
nathanheisey@5921
   158
	rect->w = (int)rc.Width() + 1;
nathanheisey@5921
   159
	rect->h = (int)rc.Height() + 1;
nathanheisey@5921
   160
	return 0;
nathanheisey@5921
   161
}
nathanheisey@5921
   162
nathanheisey@5921
   163
void BE_GetDisplayModes(_THIS, SDL_VideoDisplay *display) {
nathanheisey@5921
   164
	printf("Get Display Modes\n");
nathanheisey@5921
   165
	/* Get the current screen */
nathanheisey@5921
   166
	BScreen bscreen;
nathanheisey@5921
   167
	
nathanheisey@5921
   168
	/* Iterate through all of the modes */
nathanheisey@5921
   169
	SDL_DisplayMode mode;
nathanheisey@5921
   170
	display_mode this_bmode;
nathanheisey@5921
   171
	display_mode *bmodes;
nathanheisey@5921
   172
	uint32 count, i;
nathanheisey@5921
   173
	
nathanheisey@5921
   174
	/* Get graphics-hardware supported modes */
nathanheisey@5921
   175
	bscreen.GetModeList(&bmodes, &count);
nathanheisey@5921
   176
	bscreen.GetMode(&this_bmode);
nathanheisey@5921
   177
	
nathanheisey@5921
   178
	for(i = 0; i < count; ++i) {
nathanheisey@5921
   179
		//FIXME: Apparently there are errors with colorspace changes
nathanheisey@5921
   180
		if (bmodes[i].space == this_bmode.space) {
nathanheisey@5921
   181
			BE_BDisplayModeToSdlDisplayMode(&bmodes[i], &mode);
nathanheisey@5921
   182
			SDL_AddDisplayMode(display, &mode);
nathanheisey@5918
   183
		}
nathanheisey@5918
   184
	}
nathanheisey@5921
   185
	free(bmodes);
nathanheisey@5921
   186
}
nathanheisey@5918
   187
nathanheisey@5921
   188
int BE_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode){
nathanheisey@5921
   189
	printf("Set Display Modes\n");
nathanheisey@5921
   190
	/* Get the current screen */
nathanheisey@5921
   191
	BScreen bscreen;
nathanheisey@5921
   192
	
nathanheisey@5921
   193
	/* Set the mode using the driver data */
nathanheisey@5921
   194
	display_mode *bmode = (display_mode*)mode->driverdata;
nathanheisey@5921
   195
	if(bscreen.SetMode(bmode) == B_OK) {
nathanheisey@5921
   196
		return 0;	/* No error */
nathanheisey@5918
   197
	}
nathanheisey@5921
   198
	
nathanheisey@5921
   199
	return -1;
nathanheisey@5921
   200
}
nathanheisey@5921
   201
nathanheisey@5921
   202
#ifdef __cplusplus
nathanheisey@5921
   203
}
nathanheisey@5918
   204
#endif