Created
October 4, 2014 05:38
-
-
Save jsimmons/83231574a58d15f7c13b to your computer and use it in GitHub Desktop.
Resize Cursors SDL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define STB_IMAGE_IMPLEMENTATION | |
#include "stb_image.h" | |
#define STB_IMAGE_WRITE_IMPLEMENTATION | |
#include "stb_image_write.h" | |
#define STBIR_NO_ALPHA_EPSILON | |
#define STB_IMAGE_RESIZE_IMPLEMENTATION | |
#include "stb_image_resize.h" | |
#include <stdbool.h> | |
#include <SDL2/SDL.h> | |
struct cursor_def | |
{ | |
const char *path; | |
SDL_Surface *image; | |
SDL_Surface *image_resized; | |
SDL_Cursor *cursor; | |
SDL_Cursor *cursor_resized; | |
}; | |
struct cursor_def cursors[] = { | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_default.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_attack_default.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_item_drop.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_cannot_buy.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_spell_team.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_default_enemy.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_test.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_spell_enemy.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_move.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_spell_illegal.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_spell_default.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_coach.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_default_team.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_attack_team.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_spell_walkto.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_attack_illegal.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_learnability.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_inivisible.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_learn_ability.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_attack_enemy.bmp" , NULL, NULL, NULL, NULL}, | |
{"/home/josh/.steam/steam/SteamApps/common/dota 2 beta/dota/resource/cursor_old/cursor_buy.bmp" , NULL, NULL, NULL, NULL} | |
}; | |
#define COUNT(X) (sizeof(X) / sizeof(0[X])) | |
int main(int argc, const char **argv) | |
{ | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_Window *window = SDL_CreateWindow("cursor test", | |
SDL_WINDOWPOS_UNDEFINED, | |
SDL_WINDOWPOS_UNDEFINED, | |
800, 600, | |
0); | |
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0); | |
int factor = 2; | |
for(int i = 0; i < COUNT(cursors); i++) | |
{ | |
struct cursor_def *cursor_def = &cursors[i]; | |
int w, h, n; | |
unsigned char *data = stbi_load(cursor_def->path, &w, &h, &n, 0); | |
if(data == NULL) | |
{ | |
fprintf(stderr, "failed to load image %s: %s\n", cursor_def->path, stbi_failure_reason()); | |
exit(1); | |
} | |
int ww = w * factor, hh = h * factor; | |
unsigned char *resize_data = malloc(ww * hh * n); | |
if(stbir_resize_uint8(data, w, h, 0, resize_data, ww, hh, 0, n) == 0) | |
{ | |
fprintf(stderr, "failed to resize image %s\n", cursor_def->path); | |
exit(1); | |
} | |
SDL_Surface *surface = SDL_CreateRGBSurfaceFrom(data, w, h, 32, w * 4, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); | |
SDL_Surface *surface_resized = SDL_CreateRGBSurfaceFrom(resize_data, ww, hh, 32, ww * 4, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); | |
SDL_Cursor *cursor = SDL_CreateColorCursor(surface, 0, 0); | |
SDL_Cursor *cursor_resized = SDL_CreateColorCursor(surface_resized, 0, 0); | |
printf("%p, %p, %p, %p\n", surface, surface_resized, cursor, cursor_resized); | |
cursor_def->image = surface; | |
cursor_def->image_resized = surface_resized; | |
cursor_def->cursor = cursor; | |
cursor_def->cursor_resized = cursor_resized; | |
} | |
bool resized = false; | |
unsigned current_cursor = 0; | |
bool recreate = true; | |
for(;;) | |
{ | |
SDL_Event event; | |
while(SDL_PollEvent(&event)) | |
{ | |
switch(event.type) | |
{ | |
case SDL_QUIT: | |
goto shutdown; | |
case SDL_KEYDOWN: | |
switch(event.key.keysym.sym) | |
{ | |
case SDLK_ESCAPE: goto shutdown; | |
}; | |
break; | |
case SDL_KEYUP: | |
switch(event.key.keysym.sym) | |
{ | |
case SDLK_TAB: | |
current_cursor = (current_cursor + 1) % COUNT(cursors); | |
recreate = true; | |
break; | |
case SDLK_SPACE: | |
resized = !resized; | |
recreate = true; | |
break; | |
}; | |
break; | |
default: | |
break; | |
} | |
} | |
struct cursor_def *cursor_def = &cursors[current_cursor]; | |
if(recreate) | |
{ | |
SDL_SetCursor(resized ? cursor_def->cursor_resized : cursor_def->cursor); | |
printf("CHANGED TO %s%s\n", cursor_def->path, resized ? " resized" : ""); | |
recreate = false; | |
SDL_ShowCursor(1); | |
} | |
SDL_SetRenderDrawColor(renderer, 154, 206, 235, 255); | |
SDL_RenderClear(renderer); | |
SDL_RenderPresent(renderer); | |
SDL_Delay(1); | |
} | |
shutdown: | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment