Skip to content

Commit

Permalink
MorphOS: add BitMap to OpenGL Window
Browse files Browse the repository at this point in the history
Add MOS_GL_AllocBitmap function
Add MOS_GL_InitContext to simplify source
  • Loading branch information
BeWorld2018 committed Oct 8, 2023
1 parent 57c0ff9 commit 1a275b9
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/video/morphos/SDL_mosmodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ MOS_GetScreen(_THIS, BYTE fullscreen, SDL_bool support3d)
int use_wb_screen = 0;
ULONG openError = 0;

D("[%s] Use monitor '%s' - Screen %d\n", __FUNCTION__, data->ScrMonName ? data->ScrMonName : (STRPTR)"Workbench", data->CustomScreen);
D("[%s] support3d=%d - Use monitor '%s' - Screen %d\n", __FUNCTION__, support3d, data->ScrMonName ? data->ScrMonName : (STRPTR)"Workbench", data->CustomScreen);

if (!fullscreen && data->ScrMonName == NULL) {
data->CustomScreen = NULL;
Expand Down
182 changes: 115 additions & 67 deletions src/video/morphos/SDL_mosopengl.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,65 +93,112 @@ MOS_GL_UnloadLibrary(_THIS)
}
}

SDL_GLContext
MOS_GL_CreateContext(_THIS, SDL_Window * window)
SDL_bool MOS_GL_AllocBitmap(_THIS, SDL_Window * window)
{
D("[%s]\n", __FUNCTION__);
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;

if (data->bitmap == NULL) {
FreeBitMap(data->bitmap);
data->bitmap = NULL;
}

int sizex = window->w; //data->win->Width - data->win->BorderLeft - data->win->BorderRight;
int sizey = window->h; //data->win->Height - data->win->BorderTop - data->win->BorderBottom;
data->bitmap = AllocBitMap(sizex, sizey, data->win->RPort->BitMap->Depth, BMF_MINPLANES|BMF_DISPLAYABLE|BMF_3DTARGET, data->win->RPort->BitMap);

if (data->bitmap == NULL) {
D("[%s] Failed to create Bitmap\n", __FUNCTION__);
return SDL_FALSE;
}

return SDL_TRUE;
}

SDL_bool MOS_GL_InitContext(_THIS, SDL_Window * window, GLContext *glcont)
{

SDL_WindowData *data = (SDL_WindowData *) window->driverdata;

if (data->__tglContext != NULL) {
GLADestroyContext(data->__tglContext);
if (data->bitmap != NULL) {
FreeBitMap(data->bitmap);
data->bitmap = NULL;
}
}

struct TagItem tgltags[] =
{
{TAG_IGNORE, 0},
{TGL_CONTEXT_STENCIL, TRUE},
{TAG_DONE}
};

SDL_VideoData *vd = data->videodata;
BYTE fullscreen = data->winflags & SDL_MOS_WINDOW_FULLSCREEN;
if (fullscreen) {

tgltags[0].ti_Tag = TGL_CONTEXT_SCREEN;
tgltags[0].ti_Data = (IPTR)vd->CustomScreen;

} else {

if (MOS_GL_AllocBitmap(_this, window)) {
tgltags[0].ti_Tag = TGL_CONTEXT_BITMAP;
tgltags[0].ti_Data = (IPTR)data->bitmap;
} else {
tgltags[0].ti_Tag = TGL_CONTEXT_WINDOW;
tgltags[0].ti_Data = (IPTR)data->win;
}
}

// Initialize new context
int success = GLAInitializeContext(glcont, tgltags);
if (success) {
__tglContext = glcont;

SDL_bool withnewextension = SDL_TRUE;

char *val = val = MOS_getenv("SDL_RENDER_OPENGL_SHADERS");
if (val && strlen(val)>0)
if (strcmp(val, "0")==0)
withnewextension = SDL_FALSE;

if (withnewextension)
tglEnableNewExtensions(0); // Active TinyGL New extensions
}

return success ? SDL_TRUE : SDL_FALSE;
}

void MOS_GL_DestroyContext(_THIS)
{

}

SDL_GLContext
MOS_GL_CreateContext(_THIS, SDL_Window * window)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;

GLContext *glcont = GLInit();
D("[%s] new context 0x%08lx\n", __FUNCTION__, glcont);

if (glcont) {

int success;
struct TagItem tgltags[] =
{
{TAG_IGNORE, 0},
{TGL_CONTEXT_STENCIL, TRUE}, // TODO check if stencil and depth are needed
{TAG_DONE}
};

if (fullscreen) {
tgltags[0].ti_Tag = TGL_CONTEXT_SCREEN;
tgltags[0].ti_Data = (IPTR)vd->CustomScreen;
} if (data->win) {
tgltags[0].ti_Tag = TGL_CONTEXT_WINDOW;
tgltags[0].ti_Data = (IPTR)data->win;
} else {
// window not exist but you want createContext (example: SDL_WINDOW_HIDDEN)
// so use Ambient screen and Context Bitmap ?!
if (vd->WScreen == NULL)
MOS_GetScreen(vd->VideoDevice, 0, (window->flags & SDL_WINDOW_OPENGL) != 0);

D("[%s] new context unless window 0x%08lx - 0x%08lx\n", __FUNCTION__, glcont, vd->WScreen);
tgltags[0].ti_Tag = TGL_CONTEXT_BITMAP;
tgltags[0].ti_Data = (IPTR)vd->WScreen->RastPort.BitMap;
}

success = GLAInitializeContext(glcont, tgltags);
int success = MOS_GL_InitContext(_this, window, glcont);
if (success) {
*SDL2Base->MyGLContext = __tglContext = glcont;
data->__tglContext = glcont;
D("[%s] new context SUCCES 0x%08lx\n", __FUNCTION__, glcont);

// Disabled tglNewExtensions if SDL_RENDER_OPENGL_SHADERS is disabled (per defaut)
char *val = val = MOS_getenv("SDL_RENDER_OPENGL_SHADERS");
if (val && strlen(val)>0) {
if (strcmp(val, "0")!=0) {
D("[%s] Using tglEnableNewExtensions(0);\n", __FUNCTION__);
tglEnableNewExtensions(0);
}
} else {
D("[%s] Using tglEnableNewExtensions(0);\n", __FUNCTION__);
tglEnableNewExtensions(0);
}
*SDL2Base->MyGLContext = data->__tglContext = glcont;
D("[%s] SUCCES 0x%08lx\n", __FUNCTION__, glcont);
return glcont;
}
D("[%s] new context FAILED 0x%08lx\n", __FUNCTION__, glcont);
D("[%s] FAILED 0x%08lx\n", __FUNCTION__, glcont);
if (data->bitmap) {
FreeBitMap(data->bitmap);
data->bitmap = NULL;
}
GLClose(glcont);
data->__tglContext = NULL;
*SDL2Base->MyGLContext = data->__tglContext = __tglContext = NULL;

SDL_SetError("Couldn't initialize context TinyGL");
} else {
Expand Down Expand Up @@ -180,13 +227,14 @@ MOS_GL_GetDrawableSize(_THIS, SDL_Window * window, int *w, int *h)
*w = data->win->Width - data->win->BorderLeft - data->win->BorderRight;
if (h)
*h = data->win->Height - data->win->BorderTop - data->win->BorderBottom;

}

}

int
MOS_GL_SetSwapInterval(_THIS, int interval)
{
//D("[%s] interval=%d\n", __FUNCTION__, interval);
SDL_VideoData *data = _this->driverdata;

switch (interval) {
Expand Down Expand Up @@ -216,7 +264,7 @@ int
MOS_GL_SwapWindow(_THIS, SDL_Window * window)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
//D("[%s] context 0x%08lx\n", __FUNCTION__, data->__tglContext, data->win);

if (!data->win || !data->__tglContext || !__tglContext)
return -1;

Expand All @@ -229,6 +277,15 @@ MOS_GL_SwapWindow(_THIS, SDL_Window * window)
}

GLASwapBuffers(data->__tglContext);

if (data->bitmap != NULL) {

BltBitMapRastPort(data->bitmap, 0, 0, data->win->RPort, data->win->BorderLeft, data->win->BorderTop,
window->w/*(data->win->Width - data->win->BorderRight - data->win->BorderLeft)*//*GetBitMapAttr(data->bitmap, BMA_WIDTH)*/,
window->h/*(data->win->Height - data->win->BorderTop - data->win->BorderBottom)*//*GetBitMapAttr(data->bitmap, BMA_HEIGHT)*/, 0xc0);
}


return 0;
}

Expand All @@ -237,10 +294,9 @@ MOS_GL_DeleteContext(_THIS, SDL_GLContext context)
{
D("[%s] context 0x%08lx\n", __FUNCTION__, context);

if (TinyGLBase == NULL) {
D("[%s] the library is already closed\n", __FUNCTION__);
if (TinyGLBase == NULL)
return;
}

if (context) {
SDL_Window *sdlwin;

Expand All @@ -251,42 +307,34 @@ MOS_GL_DeleteContext(_THIS, SDL_GLContext context)
if (data->__tglContext == context) {
D("[%s] Found TinyGL context 0x%08lx, clearing window binding\n", __FUNCTION__, context);
GLADestroyContext(context);

if (data->bitmap != NULL)
FreeBitMap(data->bitmap);

GLClose(context);
data->__tglContext = NULL;
}
}
*SDL2Base->MyGLContext = __tglContext = NULL;
}
}

int
MOS_GL_ResizeContext(_THIS, SDL_Window *window)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
SDL_VideoData *vd = data->videodata;
int success;
SDL_bool success;

if (data->__tglContext == NULL || __tglContext == NULL) {
// only if __tglContext exists
D("[%s] no OpenGL context\n", __FUNCTION__);
return -1;
}
if (data->win == NULL) {
D("[%s] no window\n", __FUNCTION__);
return -1;
}

if (vd->CustomScreen) {
struct TagItem tgltags[] =
{
{TGL_CONTEXT_SCREEN, (IPTR)vd->CustomScreen},
{TGL_CONTEXT_STENCIL, TRUE}, // TODO check if stencil and depth are needed
{TAG_DONE}
};
GLADestroyContext(data->__tglContext);
success = GLAInitializeContext(data->__tglContext, tgltags);
} else {
success = GLAReinitializeContextWindowed(data->__tglContext, data->win);
}
success = MOS_GL_InitContext(_this, window, data->__tglContext);

D("[%s] success %d\n", __FUNCTION__, success);

return success ? 0 : -1;
Expand Down
2 changes: 2 additions & 0 deletions src/video/morphos/SDL_mosopengl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ extern void MOS_GL_DeleteContext(_THIS, SDL_GLContext context);

/* Non-SDL functions */
extern int MOS_GL_ResizeContext(_THIS, SDL_Window *window);
extern SDL_bool MOS_GL_AllocBitmap(_THIS, SDL_Window * window);
extern SDL_bool MOS_GL_InitContext(_THIS, SDL_Window * window, GLContext *glcont);

#endif /* _SDL_mosopengl_h */
3 changes: 3 additions & 0 deletions src/video/morphos/SDL_moswindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ typedef struct

APTR visualinfo;
struct Menu *menu;

struct BitMap *bitmap;

} SDL_WindowData;

/* Is this window shown (not iconified) */
Expand Down

0 comments on commit 1a275b9

Please sign in to comment.