src/platform/perf-main.c (view raw)
1#include "gba-thread.h"
2#include "gba.h"
3#include "renderers/video-software.h"
4
5#include <fcntl.h>
6#include <signal.h>
7#include <sys/time.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11
12static void _GBAPerfRunloop(struct GBAThread* context, int* frames);
13static void _GBAPerfShutdown(int signal);
14
15static struct GBAThread* _thread;
16
17int main(int argc, char** argv) {
18 const char* fname = "test.rom";
19 if (argc > 1) {
20 fname = argv[1];
21 }
22 int fd = open(fname, O_RDONLY);
23 if (fd < 0) {
24 return 1;
25 }
26
27 signal(SIGINT, _GBAPerfShutdown);
28
29 struct GBAThread context;
30 struct GBAVideoSoftwareRenderer renderer;
31 GBAVideoSoftwareRendererCreate(&renderer);
32
33 renderer.outputBuffer = malloc(256 * 256 * 4);
34 renderer.outputBufferStride = 256;
35
36 context.fd = fd;
37 context.fname = fname;
38 context.useDebugger = 0;
39 context.renderer = &renderer.d;
40 context.frameskip = 0;
41 context.sync.videoFrameWait = 0;
42 context.sync.audioWait = 0;
43 context.startCallback = 0;
44 context.cleanCallback = 0;
45 context.frameCallback = 0;
46 _thread = &context;
47 GBAThreadStart(&context);
48
49 int frames = 0;
50 time_t start = time(0);
51 _GBAPerfRunloop(&context, &frames);
52 time_t end = time(0);
53 int duration = end - start;
54
55 GBAThreadJoin(&context);
56 close(fd);
57
58 free(renderer.outputBuffer);
59
60 printf("%u frames in %i seconds: %g fps (%gx)\n", frames, duration, frames / (float) duration, frames / (duration * 60.f));
61
62 return 0;
63}
64
65static void _GBAPerfRunloop(struct GBAThread* context, int* frames) {
66 struct timeval lastEcho;
67 gettimeofday(&lastEcho, 0);
68 int lastFrames = 0;
69 while (context->state < THREAD_EXITING) {
70 if (GBASyncWaitFrameStart(&context->sync, 0)) {
71 ++*frames;
72 ++lastFrames;
73 struct timeval currentTime;
74 long timeDiff;
75 gettimeofday(¤tTime, 0);
76 timeDiff = currentTime.tv_sec - lastEcho.tv_sec;
77 timeDiff *= 1000;
78 timeDiff += (currentTime.tv_usec - lastEcho.tv_usec) / 1000;
79 if (timeDiff >= 1000) {
80 printf("\033[2K\rCurrent FPS: %g (%gx)", lastFrames / (timeDiff / 1000.0f), lastFrames / (float) (60 * (timeDiff / 1000.0f)));
81 fflush(stdout);
82 lastEcho = currentTime;
83 lastFrames = 0;
84 }
85 }
86 GBASyncWaitFrameEnd(&context->sync);
87 }
88}
89
90static void _GBAPerfShutdown(int signal) {
91 pthread_mutex_lock(&_thread->stateMutex);
92 _thread->state = THREAD_EXITING;
93 pthread_mutex_unlock(&_thread->stateMutex);
94}