Add pixbuf api
This commit is contained in:
commit
3ef15b94aa
21
include/ragb_sdl.h
Normal file
21
include/ragb_sdl.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
#ifndef RAGB_SDL_H
|
||||
#define RAGB_SDL_H
|
||||
#include <r_util.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
typedef struct gb_pixel_buffer_t {
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Texture *texture;
|
||||
ut8 *buf; //buffer containing pixeldata
|
||||
ut32 color[4]; //rgb colors
|
||||
ut16 w; //x size in pixel
|
||||
ut16 h; //y size in pixel
|
||||
// ut16 xsf; //x scale factor for projecting onto the texture
|
||||
// ut16 ysf; //y scale factor for projecting onto the texture
|
||||
} GBPixBuf;
|
||||
|
||||
GBPixBuf *gb_pix_buf_new(SDL_Renderer *renderer, ut16 w, ut16 h);
|
||||
void gb_pix_buf_free(GBPixBuf *pb);
|
||||
void gb_pix_buf_set_pixel(GBPixBuf *pb, ut16 x, ut16 y, ut8 pixval);
|
||||
void gb_pix_buf_set_color(GBPixBuf *pb, ut8 color_idx, ut32 rgb);
|
||||
#endif
|
64
sdl/pixbuf.c
Normal file
64
sdl/pixbuf.c
Normal file
|
@ -0,0 +1,64 @@
|
|||
#include <ragb_sdl.h>
|
||||
#include <r_util.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
GBPixBuf *gb_pix_buf_new (SDL_Renderer *renderer, ut16 w, ut16 h) {
|
||||
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 (!texture) {
|
||||
free (pb->buf);
|
||||
free (pb);
|
||||
return NULL;
|
||||
}
|
||||
pb->w = w;
|
||||
pb->h = h;
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user