Skip to content

Commit

Permalink
some fixes for windows. also went back to SDL_Delay, because i don't …
Browse files Browse the repository at this point in the history
…really know how to pause() in windows.
  • Loading branch information
usrshare committed Dec 31, 2018
1 parent 8b3d836 commit 714594f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 78 deletions.
9 changes: 9 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include "ui_menu.h"
#endif

#ifdef _WIN32
#include <windows.h>
#endif

#ifndef __CYGWIN__
// fixing a cygwin error
extern char *optarg;
Expand Down Expand Up @@ -44,6 +48,11 @@ void exit_usage_error(char** argv) {

int main (int argc, char** argv) {

#ifdef _WIN32
setvbuf(stdout,NULL,_IONBF,0); //disable buffering for windows
//timeBeginPeriod(5); //set timer frequency to slightly higher for smoother framerates
#endif

#ifdef USE_SDL_UI
if (argc == 1) return sdl_ui_main(ui_initfunc, ui_updatefunc);
#endif
Expand Down
159 changes: 81 additions & 78 deletions src/sdl_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#ifdef _WIN32
#include <windows.h>
#endif

// generic variables

Expand Down Expand Up @@ -242,13 +245,81 @@ struct sdl_frame_cb_ctx {
cb_noparam updatefunc;
};

int rerender = 0;
int sdl_ui_main(cb_noparam mainfunc, cb_noparam updatefunc) {

memset(keyboard_buffer,0,sizeof keyboard_buffer);
memset(keys_held,0,sizeof keys_held);

int r = SDL_Init(SDL_INIT_VIDEO);
if (r != 0) {
fprintf(stderr,"Unable to start SDL video: %s\n",SDL_GetError()); exit(1); }

atexit(SDL_Quit);

#ifdef NESMODE
#define WINDOWTITLE "nescityeditor"
#else
#define WINDOWTITLE "snescityeditor"
#endif

mainwin = SDL_CreateWindow(WINDOWTITLE,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,win_w,win_h,SDL_WINDOW_RESIZABLE);

if (!mainwin) {
fprintf(stderr,"Unable to create window: %s\n",SDL_GetError()); exit(1); }

ren = SDL_CreateRenderer(mainwin,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
//SDL_RenderSetLogicalSize(ren, 256, 224);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");

if (!ren) {
fprintf(stderr,"Unable to create renderer: %s\n",SDL_GetError()); exit(1); }


scs = SDL_CreateRGBSurface(0,256,224,32,0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);

if (!scs) {
fprintf(stderr,"Unable to create surface: %s\n",SDL_GetError()); exit(1); }

uint32_t sdl_frame_cb(uint32_t interval, void* param) {
#ifdef PRESCALE

SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY,"0");

pre_tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, SCREENW, SCREENH);

if (!pre_tex) {
fprintf(stderr,"Unable to create texture: %s\n",SDL_GetError()); exit(1); }

SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY,"2");

struct sdl_frame_cb_ctx* ctx = param;
tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET,output_w,output_h);
#else

SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY,"2");

tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING,SCREENW,SCREENH);

#endif


if (!tex) {
fprintf(stderr,"Unable to create texture: %s\n",SDL_GetError()); exit(1); }

tsurf = IMG_ReadXPMFromArray(textures_xpm);

mainfunc();

start_ticks = SDL_GetTicks();
framecnt = 0;

struct sdl_frame_cb_ctx ctx = {.updatefunc = updatefunc};

//----------------------------------------------------

int rerender = 0;
uint32_t oldticks = 0;

while (1) {

SDL_Event lastevent;

while (SDL_PollEvent(&lastevent) != 0) {
Expand Down Expand Up @@ -347,11 +418,12 @@ uint32_t sdl_frame_cb(uint32_t interval, void* param) {
while (frameticks <= cticks) { //it's about time

cticks = SDL_GetTicks();
ctx->updatefunc();
updatefunc();
mousecoords.b_press = mousecoords.b_release = 0;
rerender = 1;
//fillrect(0xFF0000, (framecnt % 256), 0, 1, 16);
//printf("frame %u performed at %u\n", framecnt, cticks);
//printf("frame %u performed at %u (%+d)\n", framecnt, cticks, cticks - oldticks);
oldticks = cticks;
framecnt++;
frameticks = start_ticks + (framecnt * 1000 / 60); //figure out the next frame instead
}
Expand All @@ -373,81 +445,12 @@ uint32_t sdl_frame_cb(uint32_t interval, void* param) {

rerender = 0;
}

return (frameticks - cticks);
}

int sdl_ui_main(cb_noparam mainfunc, cb_noparam updatefunc) {

memset(keyboard_buffer,0,sizeof keyboard_buffer);
memset(keys_held,0,sizeof keys_held);

int r = SDL_Init(SDL_INIT_VIDEO);
if (r != 0) {
fprintf(stderr,"Unable to start SDL video: %s\n",SDL_GetError()); exit(1); }

atexit(SDL_Quit);

#ifdef NESMODE
#define WINDOWTITLE "nescityeditor"
#else
#define WINDOWTITLE "snescityeditor"
#endif

mainwin = SDL_CreateWindow(WINDOWTITLE,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,win_w,win_h,SDL_WINDOW_RESIZABLE);

if (!mainwin) {
fprintf(stderr,"Unable to create window: %s\n",SDL_GetError()); exit(1); }

ren = SDL_CreateRenderer(mainwin,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
//SDL_RenderSetLogicalSize(ren, 256, 224);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");

if (!ren) {
fprintf(stderr,"Unable to create renderer: %s\n",SDL_GetError()); exit(1); }


scs = SDL_CreateRGBSurface(0,256,224,32,0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);

if (!scs) {
fprintf(stderr,"Unable to create surface: %s\n",SDL_GetError()); exit(1); }

#ifdef PRESCALE

SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY,"0");

pre_tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, SCREENW, SCREENH);

if (!pre_tex) {
fprintf(stderr,"Unable to create texture: %s\n",SDL_GetError()); exit(1); }

SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY,"2");

tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET,output_w,output_h);
#else

SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY,"2");

tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING,SCREENW,SCREENH);

#endif


if (!tex) {
fprintf(stderr,"Unable to create texture: %s\n",SDL_GetError()); exit(1); }

tsurf = IMG_ReadXPMFromArray(textures_xpm);

mainfunc();

start_ticks = SDL_GetTicks();
framecnt = 0;

struct sdl_frame_cb_ctx ctx = {.updatefunc = updatefunc};

SDL_AddTimer( 1000 / 60, sdl_frame_cb, &ctx);
//----------------------------------------------------

SDL_Delay (frameticks - cticks);

pause();
}

SDL_DestroyRenderer(ren);
SDL_DestroyWindow(mainwin);
Expand Down

0 comments on commit 714594f

Please sign in to comment.