Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve oslLockImage and oslUnlockImage functions for clarity and readability #51

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 75 additions & 45 deletions src/image/oslLockImage.c
Original file line number Diff line number Diff line change
@@ -1,56 +1,86 @@
#include "oslib.h"

/*
Fonctions sp�ciales PC
Permet d'acc�der en soft � une image s�lectionn�e comme drawbuffer ou texture
Special PC Functions
Allows access to an image selected as a draw buffer or texture
*/
void oslLockImage(OSL_IMAGE *img)
{
//Drawbuffer courant?
if (img == osl_curBuf) {
oslSyncDrawing();
//Avec OpenGL, il faut copier le contenu du drawbuffer sur l'image (vu qu'on va l'utiliser)
#ifndef PSP
//On doit d�sactiver (temporairement) le texturage 2D
int bTextureEnabled = glIsEnabled(GL_TEXTURE_2D), bBlendingEnabled = glIsEnabled(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
//Lit les donn�es du backbuffer dans l'image utilis�e jusque l� comme drawbuffer
emuGlReadPixels(0, 272-osl_curBuf->sizeY, osl_curBuf->sysSizeX, osl_curBuf->sizeY, GL_RGBA, osl_curBuf->pixelFormat, osl_curBuf->data);
//R�active si jamais
if (bTextureEnabled)
glEnable(GL_TEXTURE_2D);
if (bBlendingEnabled)
glEnable(GL_BLEND);
#endif
}
// Check if the image is the current draw buffer
if (img == osl_curBuf) {
// Synchronize drawing before accessing the draw buffer
oslSyncDrawing();

#ifndef PSP
// On non-PSP platforms, copy the contents of the draw buffer to the image
int textureEnabled = glIsEnabled(GL_TEXTURE_2D);
int blendingEnabled = glIsEnabled(GL_BLEND);

// Temporarily disable 2D texturing and blending
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);

// Read the pixels from the back buffer into the image data
emuGlReadPixels(
0,
272 - osl_curBuf->sizeY,
osl_curBuf->sysSizeX,
osl_curBuf->sizeY,
GL_RGBA,
osl_curBuf->pixelFormat,
osl_curBuf->data
);

// Re-enable texturing and blending if they were previously enabled
if (textureEnabled) {
glEnable(GL_TEXTURE_2D);
}
if (blendingEnabled) {
glEnable(GL_BLEND);
}
#endif
}
}

//A appeler une fois termin�
// Call this function once the image operations are complete
void oslUnlockImage(OSL_IMAGE *img)
{
// if (img->location == OSL_IN_RAM)
oslUncacheImage(img);
//Texture courante?
if (img->data == osl_curTexture)
osl_curTexture = NULL;
//Avec OpenGL, on recopie le contenu de l'image (modifi�e) vers le framebuffer
#ifndef PSP
if (img == osl_curBuf) {
//On doit d�sactiver (temporairement) le texturage 2D
int bTextureEnabled = glIsEnabled(GL_TEXTURE_2D), bBlendingEnabled = glIsEnabled(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
//Lit les donn�es du backbuffer dans l'image utilis�e jusque l� comme drawbuffer
glRasterPos2i(0, 0);
glPixelZoom(1, -1);
glDrawPixels(img->sysSizeX, img->sizeY, GL_RGBA, emu_pixelPhysFormats[img->pixelFormat], img->data);
//R�active si jamais
if (bTextureEnabled)
glEnable(GL_TEXTURE_2D);
if (bBlendingEnabled)
glEnable(GL_BLEND);
}
#endif
}
// Uncache the image if it is stored in RAM
oslUncacheImage(img);

// Check if the image is the current texture
if (img->data == osl_curTexture) {
osl_curTexture = NULL;
}

#ifndef PSP
// On non-PSP platforms, copy the modified image data back to the framebuffer
if (img == osl_curBuf) {
int textureEnabled = glIsEnabled(GL_TEXTURE_2D);
int blendingEnabled = glIsEnabled(GL_BLEND);

// Temporarily disable 2D texturing and blending
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);

// Write the image data back to the framebuffer
glRasterPos2i(0, 0);
glPixelZoom(1, -1);
glDrawPixels(
img->sysSizeX,
img->sizeY,
GL_RGBA,
emu_pixelPhysFormats[img->pixelFormat],
img->data
);

// Re-enable texturing and blending if they were previously enabled
if (textureEnabled) {
glEnable(GL_TEXTURE_2D);
}
if (blendingEnabled) {
glEnable(GL_BLEND);
}
}
#endif
}