Added uiInit(), a function that creates ui_hotbar_surface, blits the appropriate ui.png sprites to it, then sets the opacity to 0.75. At the moment, ui.png must be in RGBA format due to my lack of knowledge on alpha blending, etc. - this shall be changed later for optimization. Added the SDL_SetOpacity(SDL_Surface *surface, double opacity) function that sets the alpha layer of the target surface to the desired amount. Along with this is the uiSetOpacity helper, which simply calls SDL_SetOpacity on ui_hotbar_surface. Also added uiSetScale which simply calls interfaceScaleSurface(SDL_Surface *surface, float x, float y) on ui_hotbar_surface. interfaceScaleSurface does a rough up/down-scale of the target surface - has not been thoroughly tested for bugs, so it has the potential to break.

master
kts 2013-11-12 02:41:32 -08:00
parent 0c24fedfb2
commit d7ad2c3bd6
5 changed files with 118 additions and 6 deletions

View File

@ -35,7 +35,7 @@ xcurses: $(OBJS) $(CURSES_OBJS)
all: $(BINARY)
clean:
rm -f $(OBJS) $(CURSES_OBJS) $(SDL_OBJS) $(BINARY)
rm -f $(OBJS) tiles.o $(CURSES_OBJS) $(SDL_OBJS) $(BINARY)
rm -f pack_tiles
pack_tiles: pack_tiles.c

View File

@ -248,6 +248,9 @@ int interfaceInit() {
door_spritesheet = IMG_Load_RW(SDL_RWFromMem(&door_images, door_images_length), 1);
shadow_spritesheet = IMG_Load_RW(SDL_RWFromMem(&shadow_images, shadow_images_length), 1);
interfaceSetScale(2.0f, 2.0f);
/* set up our ui */
uiInit();
uiSetScale(1.25f, 1.25f);
// Fill our screen w/ black
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 32, 128, 64));
// Update!
@ -261,6 +264,48 @@ int interfaceInit() {
return SUCCESS;
}
int uiInit() {
int hotbar_count = 4;
/* init hotbar */
Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
ui_hotbar_surface = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCALPHA, UI_HOTBAR_ITEM_WIDTH*hotbar_count, UI_HOTBAR_ITEM_HEIGHT, 32, rmask, gmask, bmask, amask);
if(ui_hotbar_surface == NULL) {
consoleLog("SDL_CreateRGBSurface error'd on ui_hotbar_surface");
//fprintf(stderr, "CreateRGBSurface failed: %s\n", SDL_GetError());
//exit(1);
}
SDL_Rect hotbar_item_rect = { 0, 0, UI_HOTBAR_ITEM_WIDTH, UI_HOTBAR_ITEM_HEIGHT };
int i = 0;
while (i < hotbar_count) {
SDL_Rect hotbar_target_rect = {i*UI_HOTBAR_ITEM_WIDTH, 0, UI_HOTBAR_ITEM_WIDTH+i*UI_HOTBAR_ITEM_WIDTH, UI_HOTBAR_ITEM_HEIGHT};
SDL_BlitSurface(ui_spritesheet, &hotbar_item_rect, ui_hotbar_surface, &hotbar_target_rect);
i++;
}
SDL_SetOpacity(ui_hotbar_surface, 0.75);
// the rect for the actual on-screen position
}
void uiSetOpacity(double opacity) {
SDL_SetOpacity(ui_hotbar_surface);
}
void uiSetScale(float scale_x, float scale_y) {
g_ui_scale_x = scale_x;
g_ui_scale_y = scale_y;
ui_hotbar_surface = interfaceScaleSurface(ui_hotbar_surface, scale_x, scale_y);
}
/* draw the player's current view */
void cameraDraw() {
SDL_Rect camera_rect = {0, 0, screen->w, screen->h};
@ -330,7 +375,7 @@ void cameraDraw() {
}
void uiDraw() {
int hotbar_count = 4;
/* int hotbar_count = 4;
// TODO: move the next line to a uiSetAlpha function (so user can customize).
// TODO: create a Surface in interfaceInit() and simply blit that here.
SDL_SetAlpha(ui_spritesheet, SDL_SRCALPHA, 200);
@ -342,7 +387,10 @@ void uiDraw() {
SDL_Rect hotbar_target_rect = {(screen->w/2 - hotbar_width/2)+i*32, screen->h-hotbar_height, (screen->w/2 - hotbar_width/2)+32+i*32, screen->h};
SDL_BlitSurface(ui_spritesheet, &hotbar_item_rect, screen, &hotbar_target_rect);
i++;
}
}*/
SDL_Rect ui_hotbar_target_rect = {(screen->w/2 - ui_hotbar_surface->w/2), screen->h-ui_hotbar_surface->h, (screen->w/2 - ui_hotbar_surface->w/2), screen->h};
SDL_BlitSurface(ui_hotbar_surface, NULL, screen, &ui_hotbar_target_rect);
//SDL_BlitSurface(ui_hotbar_surface, NULL, screen, NULL);
}
int interfaceLoop() {
@ -564,6 +612,56 @@ if(!Surface || !Width || !Height)
return _ret;
}*/
void SDL_SetOpacity(SDL_Surface *surface, double percent) {
Uint8 surface_bpp = surface->format->BytesPerPixel;
Uint8 *surface_bits;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
int amask = 0x000000ff;
int cmask = 0xffffff00;
#else
int amask = 0xff000000;
int cmask = 0x00ffffff;
int shift = 24;
#endif
int x;
int y;
Uint32 pixels;
Uint32 alpha;
for(y=0;y<surface->h;y++) {
for(x=0;x<surface->w;x++) {
surface_bits = ((Uint8 *)surface->pixels+(y*surface->pitch)+(x*surface_bpp));
pixels = *((Uint32 *)(surface_bits));
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
alpha = pixels & mask;
alpha*=percent;
*((Uint32 *)(surface_bits)) = (pixels & cmask)|alpha;
#else
alpha = (pixels&amask)>>shift;
alpha*=percent;
*((Uint32 *)(surface_bits)) = (pixels & cmask)|(alpha<<shift);
#endif
}
}
}
void SDL_CopySurface(SDL_Surface *from_surface, SDL_Rect *from_rect, SDL_Surface *to_surface, SDL_Rect *to_rect) {
Sint16 x;
Sint16 y;
Sint16 from_x = from_rect->x;
Sint16 from_y = from_rect->y;
Sint16 to_x = to_rect->x;
Sint16 to_y = to_rect->y;
SDL_LockSurface(from_surface);
SDL_LockSurface(to_surface);
for (y = 0;y < from_rect->h;y++) {
for (x = 0;x < from_rect->w;x++) {
putpixel(to_surface, to_x+x, to_y+y, getpixel(from_surface, from_x+x, from_y+y));
}
}
SDL_UnlockSurface(from_surface);
SDL_UnlockSurface(to_surface);
}
SDL_Surface *interfaceScaleSurface(SDL_Surface *surface, float scale_x, float scale_y) {
long x;
long y;

View File

@ -38,8 +38,20 @@ float g_tile_width;
float g_tile_height;
void interfaceSetScale(float scale_x, float scale_y);
void SDL_CopySurface(SDL_Surface *from_surface, SDL_Rect *from_rect, SDL_Surface *to_surface, SDL_Rect *to_rect);
float g_ui_scale_x;
float g_ui_scale_y;
#define UI_HOTBAR_ITEM_HEIGHT 32
#define UI_HOTBAR_ITEM_WIDTH 32
float g_hotbar_scale_x;
float g_hotbar_scale_y;
SDL_Surface *ui_hotbar_surface;
SDL_Rect *ui_hotbar_target_rect;
int uiInit();
void uiDraw();
void uiSetScale(float scale_x, float scale_y);
void interfaceDrawString(const char *string, int start_x, int start_y);
void interfaceDrawChar(char ch, int start_x, int start_y);

2
tile.c
View File

@ -109,5 +109,5 @@ struct PlayerTile players[] = {
//{ collision, name, target tile, behavior, basevision}
struct NpcTile npcs[] = {
{ NO_PASS, "nupi", 0, BEHAVE_WANDER, 4}
{ NO_PASS, "nupi", 4, BEHAVE_WANDER, 0, 0}
};

6
tile.h
View File

@ -48,6 +48,7 @@ struct PlayerTile {
int collision;
char name[16];
int vision;
struct Tile *inventory;
struct Controller *controller;
};
extern struct PlayerTile players[];
@ -55,9 +56,10 @@ extern struct PlayerTile players[];
struct NpcTile {
int collision;
char name[16];
struct Tile *target;
int behavior; // for BEHAVE_AGGRESSIVE, etc.
int vision;
int behavior; // for BEHAVE_AGGRESSIVE, etc.
struct Tile *inventory;
struct Tile *target;
};
extern struct NpcTile npcs[];
/*