106 lines
3.6 KiB
C
106 lines
3.6 KiB
C
/******
|
|
SDL "software" renderer functions
|
|
******/
|
|
#include "../ui/elements.h"
|
|
#include "r_soft.h"
|
|
#include "interface.h"
|
|
|
|
void r_soft_Init() {
|
|
if ((screen = SDL_SetVideoMode(g_video_width, g_video_height, 32, SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE|g_video_fullscreen)) == NULL)
|
|
return;
|
|
}
|
|
|
|
void r_soft_Reinit() {
|
|
if ((screen = SDL_SetVideoMode(g_video_width, g_video_height, 32, SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE|g_video_fullscreen)) == NULL)
|
|
return;
|
|
}
|
|
|
|
void r_soft_clearScreen() {
|
|
/* struct Element *current = g_elements->first;
|
|
while(current) {
|
|
struct Element *next = current->next;
|
|
if (current->flags & E_FLAG_UPDATE) {
|
|
SDL_Rect rect = { current->dimen.x, current->dimen.y, current->dimen.w, current->dimen.h };
|
|
SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 32, 128, 64));
|
|
}
|
|
current = next;
|
|
}*/
|
|
SDL_FillRect(screen, NULL, 0); // clear our surface
|
|
}
|
|
|
|
void r_soft_renderScreen() {
|
|
if (SDL_Flip(screen) != 0) {
|
|
printf("ERROR: %s\n", SDL_GetError());
|
|
}
|
|
}
|
|
|
|
/* the blit functions blit the Element's self surface to the surface pointed to by target. */
|
|
void r_soft_renderElements(struct ElementList *list) {
|
|
struct Element *current = list->first;
|
|
while(current != NULL) {
|
|
struct Element *next = current->next;
|
|
r_soft_renderElement(current);
|
|
current = next;
|
|
}
|
|
}
|
|
|
|
void r_soft_renderElement(struct Element *element) {
|
|
if (element == NULL)
|
|
return;
|
|
if (element->flags & E_FLAG_HIDE || element->target == NULL)
|
|
return;
|
|
if (element->flags & E_FLAG_UPDATE)
|
|
r_soft_drawElement(element);
|
|
SDL_Rect rect = { element->dimen.x, element->dimen.y, element->dimen.w, element->dimen.h };
|
|
SDL_BlitSurface(element->self, NULL, element->target, &rect);
|
|
}
|
|
|
|
/* draw functions. Creates an SDL_Surface pointed to by the Element's self void pointer then draws to that surface */
|
|
|
|
void r_soft_drawElement(struct Element *element) {
|
|
SDL_Rect rect = { element->dimen.x, element->dimen.y, element->dimen.w, element->dimen.h };
|
|
if (element->self == NULL) {
|
|
SDL_Surface *e_surface = element->target;
|
|
element->self = SDL_CreateRGBSurface(e_surface->flags, element->dimen.w, element->dimen.h, e_surface->format->BitsPerPixel, e_surface->format->Rmask, e_surface->format->Gmask, e_surface->format->Bmask, screen->format->Amask);
|
|
SDL_SetColorKey(element->self, SDL_SRCCOLORKEY, 0xFF00FF);
|
|
}
|
|
SDL_FillRect(element->self, NULL, 0XFF00FF); // clear our surface
|
|
/* draw our element's surface based upon type and state */
|
|
switch (element->type) {
|
|
case E_TYPE_TEXT:
|
|
r_soft_drawText(element->self, element->font, ((struct TextElement*)element->data)->string);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
/* now that we've redrawn the element, remove update flag */
|
|
element->flags &= ~(E_FLAG_UPDATE);
|
|
}
|
|
|
|
void r_soft_drawText(SDL_Surface *surface, struct Font *font, const char *string) {
|
|
if (font == NULL) return;
|
|
int i = 0;
|
|
int x_offset;
|
|
int y_offset;
|
|
int x_render = 0;
|
|
int y_render = 0;
|
|
//SDL_SetColorKey(font->s_surface, SDL_SRCCOLORKEY, 0xFF00FF);
|
|
while(string[i] != '\0') {
|
|
switch(string[i]) {
|
|
case '\n':
|
|
y_render += font->s_height;
|
|
x_render = 0;
|
|
break;
|
|
default:
|
|
y_offset = string[i] / 16;
|
|
x_offset = string[i] - (y_offset*16);
|
|
SDL_Rect character_offset = { x_offset * font->s_width, y_offset * font->s_height, font->s_width, font->s_height };
|
|
SDL_Rect render_position = { x_render, y_render, font->s_width, font->s_height };
|
|
SDL_BlitSurface(font->s_surface, &character_offset, surface, &render_position);
|
|
x_render += font->s_width;
|
|
break;
|
|
}
|
|
i++;
|
|
}
|
|
}
|