limit cpu usage

This commit is contained in:
lif 2024-03-03 01:25:14 -08:00
parent f3236168f0
commit cbac106862
1 changed files with 26 additions and 13 deletions

33
main.c
View File

@ -1,24 +1,26 @@
#include <stdint.h>
#define SDL_MAIN_USE_CALLBACKS 1
#include "SDL3/SDL_blendmode.h" #include "SDL3/SDL_blendmode.h"
#include "SDL3/SDL_error.h" #include "SDL3/SDL_error.h"
#include "SDL3/SDL_stdinc.h"
#include "SDL3/SDL_surface.h"
#include "mgba/core/config.h"
#include <stdint.h>
#define SDL_MAIN_USE_CALLBACKS 1
#include "SDL3/SDL_init.h" #include "SDL3/SDL_init.h"
#include "SDL3/SDL_log.h" #include "SDL3/SDL_log.h"
#include "SDL3/SDL_main.h" #include "SDL3/SDL_main.h"
#include "SDL3/SDL_pixels.h" #include "SDL3/SDL_pixels.h"
#include "SDL3/SDL_render.h" #include "SDL3/SDL_render.h"
#include "SDL3/SDL_stdinc.h"
#include "SDL3/SDL_surface.h"
#include "SDL3/SDL_timer.h" #include "SDL3/SDL_timer.h"
#include "SDL3/SDL_video.h" #include "SDL3/SDL_video.h"
#include "mgba/core/config.h"
#include "mgba/core/core.h" #include "mgba/core/core.h"
#include "mgba/core/interface.h" #include "mgba/core/interface.h"
#include "mgba/feature/commandline.h" #include "mgba/feature/commandline.h"
#include "mgba-util/image.h" #include "mgba-util/image.h"
#define NUM_CHANNELS 3 #define NUM_CHANNELS 3
#define FRAMESKIP_LIMIT 20
static SDL_Window* window = NULL; static SDL_Window* window = NULL;
static SDL_Renderer* renderer = NULL; static SDL_Renderer* renderer = NULL;
@ -62,11 +64,7 @@ void myStartRequestImageRed(struct mImageSource* self, unsigned w, unsigned h, i
SDL_DestroySurface(filtered[0]); SDL_DestroySurface(filtered[0]);
scaled = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_XBGR1555); scaled = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_XBGR1555);
filtered[0] = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_XBGR1555); filtered[0] = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_XBGR1555);
SDL_Log("red: %d x %d", w, h); SDL_Log("red: %d x %d", w, h);
// SDL_DestroyTexture(texture);
// texture = SDL_CreateTextureFromSurface(renderer, scaled);
} }
void myStartRequestImageGreen(struct mImageSource* self, unsigned w, unsigned h, int colorFormats) { void myStartRequestImageGreen(struct mImageSource* self, unsigned w, unsigned h, int colorFormats) {
@ -197,7 +195,7 @@ int SDL_AppInit(int argc, char *argv[]) {
return -1; return -1;
} }
renderer = SDL_CreateRenderer(window, NULL, 0); renderer = SDL_CreateRenderer(window, NULL, SDL_RENDERER_PRESENTVSYNC);
if (!renderer) { if (!renderer) {
SDL_Log("Couldn't create renderer: %s", SDL_GetError()); SDL_Log("Couldn't create renderer: %s", SDL_GetError());
return -1; return -1;
@ -383,6 +381,7 @@ int SDL_AppEvent(const SDL_Event *event) {
int SDL_AppIterate(void) { int SDL_AppIterate(void) {
int i; int i;
static int since_last_present = 0;
if (scaled != NULL) { if (scaled != NULL) {
Uint64 timestampNS = 0; Uint64 timestampNS = 0;
@ -420,10 +419,12 @@ int SDL_AppIterate(void) {
} }
} }
if (since_last_present < FRAMESKIP_LIMIT) {
for (i = 0; i < NUM_CHANNELS; i++) { for (i = 0; i < NUM_CHANNELS; i++) {
struct mCore* core = cores[i]; struct mCore* core = cores[i];
core->runFrame(core); core->runFrame(core);
} }
}
if (!texture_updated) { if (!texture_updated) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
@ -439,6 +440,18 @@ int SDL_AppIterate(void) {
} }
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
since_last_present = 0;
} else {
since_last_present++;
if (since_last_present >= FRAMESKIP_LIMIT) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
for (i = 0; i < NUM_CHANNELS; i++) {
SDL_Texture *texture = textures[i];
SDL_RenderTexture(renderer, texture, NULL, NULL);
}
SDL_RenderPresent(renderer);
}
} }
return 0; /* keep iterating. */ return 0; /* keep iterating. */