small optimizations and speed limiter

This commit is contained in:
erysdren 2024-09-07 13:56:42 -05:00
parent 10334a2fe2
commit e318038efc
2 changed files with 31 additions and 10 deletions

View file

@ -37,6 +37,7 @@ SOFTWARE.
#define WIDTH (640)
#define HEIGHT (480)
#define TITLE "small bitmap editor | by erysdren"
#define PIXEL_WIDTH (7)
#define PIXEL_HEIGHT (7)
#define BITMAP_WIDTH (64)
@ -248,8 +249,29 @@ static SDL_Surface *surface32;
static SDL_Renderer *renderer;
static SDL_Texture *texture;
static SDL_Rect blit_rect;
static SDL_bool running;
static SDL_Color colors[256];
static Uint64 next_time = 0;
#define DELAY (15)
static Uint64 time_left(void)
{
Uint64 now = SDL_GetTicks64();
if (next_time <= now)
return 0;
else
return next_time - now;
}
static void speed_limiter(void)
{
if (next_time == 0)
next_time = SDL_GetTicks64() + DELAY;
SDL_Delay(time_left());
next_time += DELAY;
}
/* push bitmap to stack */
void bitmap_push(int redo)
@ -887,7 +909,7 @@ int main(int argc, char **argv)
SDL_Init(SDL_INIT_VIDEO);
/* create window */
window = SDL_CreateWindow("editor",
window = SDL_CreateWindow(TITLE,
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WIDTH, HEIGHT,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
@ -941,8 +963,7 @@ int main(int argc, char **argv)
current_color = 63;
/* main loop */
running = SDL_TRUE;
while (running)
while (1)
{
/* parse sdl events */
while (SDL_PollEvent(&event))
@ -952,8 +973,7 @@ int main(int argc, char **argv)
switch (event.type)
{
case SDL_QUIT:
running = SDL_FALSE;
break;
goto done;
}
}
@ -1246,9 +1266,13 @@ int main(int argc, char **argv)
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
/* run speed limiter */
speed_limiter();
}
/* quit */
done:
SDL_FreeSurface(surface8);
SDL_FreeSurface(surface32);
SDL_DestroyTexture(texture);

View file

@ -15,7 +15,7 @@ STRIP ?= strip
PKGCONFIG ?= pkg-config
endif
override CFLAGS += $(shell $(PKGCONFIG) sdl2 --cflags) -DEUI_PIXEL_DEPTH=8 -pedantic -Wextra -Wall
override CFLAGS += $(shell $(PKGCONFIG) sdl2 --cflags) -DEUI_PIXEL_DEPTH=8 -pedantic -Wextra -Wall -Ofast
override LDFLAGS += $(shell $(PKGCONFIG) sdl2 --libs)
OBJECTS = editor.o eui_sdl2.o eui.o tinyfiledialogs.o
@ -28,6 +28,3 @@ clean:
$(EXEC): $(OBJECTS)
$(CC) -o $@ $^ $(LDFLAGS)
$(STRIP) $(EXEC)
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)