From ed79ed1672ea0ce23ea236a322d8e8be6ae85815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edu=20Garc=C3=ADa?= <28616+Arcnor@users.noreply.github.com> Date: Wed, 8 May 2024 17:56:00 +0100 Subject: [PATCH] [gl] extracted some common GPU rendering code --- renderer/HardwareBaseGPU.cpp | 152 ++++++++++++++++++++++++++++++++++ renderer/HardwareInternal.h | 2 + renderer/HardwareOpenGL.cpp | 154 +---------------------------------- 3 files changed, 157 insertions(+), 151 deletions(-) diff --git a/renderer/HardwareBaseGPU.cpp b/renderer/HardwareBaseGPU.cpp index 26d4e955..70f5cced 100644 --- a/renderer/HardwareBaseGPU.cpp +++ b/renderer/HardwareBaseGPU.cpp @@ -109,6 +109,158 @@ void rend_SetAlphaFactor(float val) { // Returns the current Alpha factor float rend_GetAlphaFactor(void) { return gpu_Alpha_factor; } +// Gets a pointer to a linear frame buffer +void rend_GetLFBLock(renderer_lfb *lfb) {} + +// Releases an lfb lock +void rend_ReleaseLFBLock(renderer_lfb *lfb) {} + +// Returns the aspect ratio of the physical screen +void rend_GetProjectionParameters(int *width, int *height) { + *width = gpu_state.clip_x2 - gpu_state.clip_x1; + *height = gpu_state.clip_y2 - gpu_state.clip_y1; +} + +void rend_GetProjectionScreenParameters(int &screenLX, int &screenTY, int &screenW, int &screenH) { + screenLX = gpu_state.clip_x1; + screenTY = gpu_state.clip_y1; + screenW = gpu_state.clip_x2 - gpu_state.clip_x1 + 1; + screenH = gpu_state.clip_y2 - gpu_state.clip_y1 + 1; +} + +// Returns the aspect ratio of the physical screen +float rend_GetAspectRatio(void) { + float aspect_ratio = (float)((3.0f * gpu_state.screen_width) / (4.0f * gpu_state.screen_height)); + return aspect_ratio; +} + +// Given a source x,y and width,height, draws any sized bitmap into the renderer lfb +void rend_DrawLFBBitmap(int sx, int sy, int w, int h, int dx, int dy, ushort *data, int rowsize) {} + +// given a chunked bitmap, renders it. +void rend_DrawChunkedBitmap(chunked_bitmap *chunk, int x, int y, ubyte alpha) { + int *bm_array = chunk->bm_array; + int w = chunk->w; + int h = chunk->h; + int piece_w = bm_w(bm_array[0], 0); + int piece_h = bm_h(bm_array[0], 0); + int screen_w, screen_h; + int i, t; + rend_SetZBufferState(0); + rend_GetProjectionParameters(&screen_w, &screen_h); + for (i = 0; i < h; i++) { + for (t = 0; t < w; t++) { + int dx = x + (piece_w * t); + int dy = y + (piece_h * i); + int dw, dh; + if ((dx + piece_w) > screen_w) + dw = piece_w - ((dx + piece_w) - screen_w); + else + dw = piece_w; + if ((dy + piece_h) > screen_h) + dh = piece_h - ((dy + piece_h) - screen_h); + else + dh = piece_h; + + float u2 = (float)dw / (float)piece_w; + float v2 = (float)dh / (float)piece_h; + rend_DrawSimpleBitmap(bm_array[i * w + t], dx, dy); + } + } + rend_SetZBufferState(1); +} + +// given a chunked bitmap, renders it.scaled +void rend_DrawScaledChunkedBitmap(chunked_bitmap *chunk, int x, int y, int neww, int newh, ubyte alpha) { + int *bm_array = chunk->bm_array; + int w = chunk->w; + int h = chunk->h; + int piece_w; + int piece_h; + int screen_w, screen_h; + int i, t; + + float scalew, scaleh; + + scalew = ((float)neww) / ((float)chunk->pw); + scaleh = ((float)newh) / ((float)chunk->ph); + piece_w = scalew * ((float)bm_w(bm_array[0], 0)); + piece_h = scaleh * ((float)bm_h(bm_array[0], 0)); + rend_GetProjectionParameters(&screen_w, &screen_h); + rend_SetOverlayType(OT_NONE); + rend_SetLighting(LS_NONE); + rend_SetColorModel(CM_MONO); + rend_SetZBufferState(0); + rend_SetAlphaType(AT_CONSTANT_TEXTURE); + rend_SetAlphaValue(alpha); + rend_SetWrapType(WT_WRAP); + for (i = 0; i < h; i++) { + for (t = 0; t < w; t++) { + int dx = x + (piece_w * t); + int dy = y + (piece_h * i); + int dw, dh; + if ((dx + piece_w) > screen_w) + dw = piece_w - ((dx + piece_w) - screen_w); + else + dw = piece_w; + if ((dy + piece_h) > screen_h) + dh = piece_h - ((dy + piece_h) - screen_h); + else + dh = piece_h; + + float u2 = (float)dw / (float)piece_w; + float v2 = (float)dh / (float)piece_h; + rend_DrawScaledBitmap(dx, dy, dx + dw, dy + dh, bm_array[i * w + t], 0, 0, u2, v2); + } + } + rend_SetZBufferState(1); +} + +// Sets some global preferences for the renderer +int rend_SetPreferredState(renderer_preferred_state *pref_state) { + int retval = 1; + renderer_preferred_state old_state = gpu_preferred_state; + + gpu_preferred_state = *pref_state; + if (gpu_state.initted) { + int reinit = 0; + mprintf((0, "Inside pref state!\n")); + + // Change gamma if needed + if (pref_state->width != gpu_state.screen_width || pref_state->height != gpu_state.screen_height || + old_state.bit_depth != pref_state->bit_depth) { + reinit = 1; + } + + if (reinit) { + retval = rend_ReInit(); + } else { + if (old_state.gamma != pref_state->gamma) { + rend_SetGammaValue(pref_state->gamma); + } + } + } else { + gpu_preferred_state = *pref_state; + } + + return retval; +} + +// Draws a simple bitmap at the specified x,y location +void rend_DrawSimpleBitmap(int bm_handle, int x, int y) { + rend_SetAlphaType(AT_CONSTANT_TEXTURE); + rend_SetAlphaValue(255); + rend_SetLighting(LS_NONE); + rend_SetColorModel(CM_MONO); + rend_SetOverlayType(OT_NONE); + rend_SetFiltering(0); + rend_DrawScaledBitmap(x, y, x + bm_w(bm_handle, 0), y + bm_h(bm_handle, 0), bm_handle, 0, 0, 1, 1); + rend_SetFiltering(1); +} + +// Fills in the passed in pointer with the current rendering state +void rend_GetRenderState(rendering_state *rstate) { memcpy(rstate, &gpu_state, sizeof(rendering_state)); } + // Preuploads a texture to the video card void rend_PreUploadTextureToCard(int handle, int map_type) {} diff --git a/renderer/HardwareInternal.h b/renderer/HardwareInternal.h index 38611679..c5d0cf6d 100644 --- a/renderer/HardwareInternal.h +++ b/renderer/HardwareInternal.h @@ -58,4 +58,6 @@ void rend_TransformSetViewport(int lx, int ty, int width, int height); void rend_TransformSetProjection(float trans[4][4]); void rend_TransformSetModelView(float trans[4][4]); +int rend_ReInit(); + #endif diff --git a/renderer/HardwareOpenGL.cpp b/renderer/HardwareOpenGL.cpp index 7795663e..182dc47d 100644 --- a/renderer/HardwareOpenGL.cpp +++ b/renderer/HardwareOpenGL.cpp @@ -2806,159 +2806,11 @@ void rend_SetZBufferWriteMask(int state) { } } -// Gets a pointer to a linear frame buffer -void rend_GetLFBLock(renderer_lfb *lfb) {} - -// Releases an lfb lock -void rend_ReleaseLFBLock(renderer_lfb *lfb) {} - -// Returns the aspect ratio of the physical screen -void rend_GetProjectionParameters(int *width, int *height) { - *width = gpu_state.clip_x2 - gpu_state.clip_x1; - *height = gpu_state.clip_y2 - gpu_state.clip_y1; +int rend_ReInit() { + opengl_Close(); + return opengl_Init(NULL, &gpu_preferred_state); } -void rend_GetProjectionScreenParameters(int &screenLX, int &screenTY, int &screenW, int &screenH) { - screenLX = gpu_state.clip_x1; - screenTY = gpu_state.clip_y1; - screenW = gpu_state.clip_x2 - gpu_state.clip_x1 + 1; - screenH = gpu_state.clip_y2 - gpu_state.clip_y1 + 1; -} - -// Returns the aspect ratio of the physical screen -float rend_GetAspectRatio(void) { - float aspect_ratio = (float)((3.0f * gpu_state.screen_width) / (4.0f * gpu_state.screen_height)); - return aspect_ratio; -} - -// Given a source x,y and width,height, draws any sized bitmap into the renderer lfb -void rend_DrawLFBBitmap(int sx, int sy, int w, int h, int dx, int dy, ushort *data, int rowsize) {} - -// given a chunked bitmap, renders it. -void rend_DrawChunkedBitmap(chunked_bitmap *chunk, int x, int y, ubyte alpha) { - int *bm_array = chunk->bm_array; - int w = chunk->w; - int h = chunk->h; - int piece_w = bm_w(bm_array[0], 0); - int piece_h = bm_h(bm_array[0], 0); - int screen_w, screen_h; - int i, t; - rend_SetZBufferState(0); - rend_GetProjectionParameters(&screen_w, &screen_h); - for (i = 0; i < h; i++) { - for (t = 0; t < w; t++) { - int dx = x + (piece_w * t); - int dy = y + (piece_h * i); - int dw, dh; - if ((dx + piece_w) > screen_w) - dw = piece_w - ((dx + piece_w) - screen_w); - else - dw = piece_w; - if ((dy + piece_h) > screen_h) - dh = piece_h - ((dy + piece_h) - screen_h); - else - dh = piece_h; - - float u2 = (float)dw / (float)piece_w; - float v2 = (float)dh / (float)piece_h; - rend_DrawSimpleBitmap(bm_array[i * w + t], dx, dy); - } - } - rend_SetZBufferState(1); -} - -// given a chunked bitmap, renders it.scaled -void rend_DrawScaledChunkedBitmap(chunked_bitmap *chunk, int x, int y, int neww, int newh, ubyte alpha) { - int *bm_array = chunk->bm_array; - int w = chunk->w; - int h = chunk->h; - int piece_w; - int piece_h; - int screen_w, screen_h; - int i, t; - - float scalew, scaleh; - - scalew = ((float)neww) / ((float)chunk->pw); - scaleh = ((float)newh) / ((float)chunk->ph); - piece_w = scalew * ((float)bm_w(bm_array[0], 0)); - piece_h = scaleh * ((float)bm_h(bm_array[0], 0)); - rend_GetProjectionParameters(&screen_w, &screen_h); - rend_SetOverlayType(OT_NONE); - rend_SetLighting(LS_NONE); - rend_SetColorModel(CM_MONO); - rend_SetZBufferState(0); - rend_SetAlphaType(AT_CONSTANT_TEXTURE); - rend_SetAlphaValue(alpha); - rend_SetWrapType(WT_WRAP); - for (i = 0; i < h; i++) { - for (t = 0; t < w; t++) { - int dx = x + (piece_w * t); - int dy = y + (piece_h * i); - int dw, dh; - if ((dx + piece_w) > screen_w) - dw = piece_w - ((dx + piece_w) - screen_w); - else - dw = piece_w; - if ((dy + piece_h) > screen_h) - dh = piece_h - ((dy + piece_h) - screen_h); - else - dh = piece_h; - - float u2 = (float)dw / (float)piece_w; - float v2 = (float)dh / (float)piece_h; - rend_DrawScaledBitmap(dx, dy, dx + dw, dy + dh, bm_array[i * w + t], 0, 0, u2, v2); - } - } - rend_SetZBufferState(1); -} - -// Sets some global preferences for the renderer -int rend_SetPreferredState(renderer_preferred_state *pref_state) { - int retval = 1; - renderer_preferred_state old_state = gpu_preferred_state; - - gpu_preferred_state = *pref_state; - if (gpu_state.initted) { - int reinit = 0; - mprintf((0, "Inside pref state!\n")); - - // Change gamma if needed - if (pref_state->width != gpu_state.screen_width || pref_state->height != gpu_state.screen_height || - old_state.bit_depth != pref_state->bit_depth) { - reinit = 1; - } - - if (reinit) { - opengl_Close(); - retval = opengl_Init(NULL, &gpu_preferred_state); - } else { - if (old_state.gamma != pref_state->gamma) { - rend_SetGammaValue(pref_state->gamma); - } - } - } else { - gpu_preferred_state = *pref_state; - } - - return retval; -} - -// Draws a simple bitmap at the specified x,y location -void rend_DrawSimpleBitmap(int bm_handle, int x, int y) { - rend_SetAlphaType(AT_CONSTANT_TEXTURE); - rend_SetAlphaValue(255); - rend_SetLighting(LS_NONE); - rend_SetColorModel(CM_MONO); - rend_SetOverlayType(OT_NONE); - rend_SetFiltering(0); - rend_DrawScaledBitmap(x, y, x + bm_w(bm_handle, 0), y + bm_h(bm_handle, 0), bm_handle, 0, 0, 1, 1); - rend_SetFiltering(1); -} - -// Fills in the passed in pointer with the current rendering state -void rend_GetRenderState(rendering_state *rstate) { memcpy(rstate, &gpu_state, sizeof(rendering_state)); } - // Takes a bitmap and blits it to the screen using linear frame buffer stuff // X and Y are the destination X,Y void rend_CopyBitmapToFramebuffer(int bm_handle, int x, int y) {