78 lines
2.0 KiB
C
78 lines
2.0 KiB
C
#include <ragb_sdl.h>
|
|
#include <r_util.h>
|
|
#include <SDL2/SDL.h>
|
|
|
|
GBPixBuf *gb_pix_buf_new (SDL_Renderer *renderer, ut16 w, ut16 h, ut32 clear_color) {
|
|
if (!renderer || !w || !h) {
|
|
return NULL;
|
|
}
|
|
GBPixBuf *pb = R_NEW (GBPixBuf);
|
|
if (!pb) {
|
|
return NULL;
|
|
}
|
|
const ut32 bitsize = w * h * 2;
|
|
const ut32 bytesize = (!!(bitsize & 0x7)) + (bitsize >> 3);
|
|
pb->buf = R_NEWS0 (ut8, bytesize);
|
|
if (!pb->buf) {
|
|
free (pb);
|
|
return NULL;
|
|
}
|
|
pb->texture = SDL_CreateTexture (renderer, SDL_PIXELFORMAT_ARGB32,
|
|
SDL_TEXTUREACCESS_TARGET, w, h);
|
|
if (!pb->texture) {
|
|
free (pb->buf);
|
|
free (pb);
|
|
return NULL;
|
|
}
|
|
pb->w = w;
|
|
pb->h = h;
|
|
pb->clear_color = clear_color;
|
|
return pb;
|
|
}
|
|
|
|
void gb_pix_buf_free (GBPixBuf *pb) {
|
|
if (!pb) {
|
|
return;
|
|
}
|
|
SDL_DestroyTexture (pb->texture);
|
|
free (pb->buf);
|
|
free (pb);
|
|
}
|
|
|
|
void gb_pix_buf_set_pixel (GBPixBuf *pb, ut16 x, ut16 y, ut8 pixval) {
|
|
if (!pb) {
|
|
return;
|
|
}
|
|
const ut32 bitpos = (pb->w * y + x) * 2;
|
|
const ut32 bytepos = bitpos >> 3;
|
|
const ut32 shl = bitpos & 0x6;
|
|
pixval = pixval & 0x3;
|
|
const ut8 mask = ~(0x3 << shl);
|
|
pb->buf[bytepos] = (pb->buf[bytepos] & mask) | (pixval << shl);
|
|
SDL_Texture *target = SDL_GetRenderTarget (pb->renderer);
|
|
SDL_SetRenderTarget (pb->renderer, pb->texture);
|
|
SDL_SetRenderDrawColor (pb->renderer, (pb->color[pixval] >> 16) & 0xff,
|
|
(pb->color[pixval] >> 8) & 0xff, pb->color[pixval] & 0xff, 0xff);
|
|
SDL_RenderDrawPoint (pb->renderer, x, y);
|
|
SDL_SetRenderTarget (pb->renderer, target);
|
|
}
|
|
|
|
void gb_pix_buf_set_color (GBPixBuf *pb, ut8 color_idx, ut32 rgb) {
|
|
if (!pb) {
|
|
return;
|
|
}
|
|
pb->color[color_idx & 0x3] = rgb;
|
|
}
|
|
|
|
void gb_pix_buf_clear (GBPixBuf *pb) {
|
|
if (!pb) {
|
|
return;
|
|
}
|
|
SDL_Texture *target = SDL_GetRenderTarget (pb->renderer);
|
|
SDL_SetRenderTarget (pb->renderer, pb->texture);
|
|
SDL_SetRenderDrawColor (pb->renderer, (pb->clear_color >> 16) & 0xff,
|
|
(pb->clear_color >> 8) & 0xff, pb->clear_color & 0xff, 0xff);
|
|
SDL_RenderClear (pb->renderer);
|
|
SDL_SetRenderTarget (pb->renderer, target);
|
|
}
|