|
| 1 | + |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <SDL2/SDL.h> |
| 5 | +#include <SDL2/SDL_test_font.h> |
| 6 | +#include <SDL2/SDL_image.h> |
| 7 | +#include "../src/SDL_shader.h" |
| 8 | +#include "../src/SDL_SYS_RenderStructs.h" |
| 9 | +#include <math.h> |
| 10 | +#ifndef M_PI |
| 11 | +#define M_PI 3.14159265358979323846 |
| 12 | +#endif /*M_PI*/ |
| 13 | + |
| 14 | +char* scaler_names[] = { |
| 15 | + "No scaler", |
| 16 | + "scale2x/MAME2x" |
| 17 | +}; |
| 18 | + |
| 19 | +int mod(int a, int b) |
| 20 | +{ |
| 21 | + int r = a % b; |
| 22 | + return r < 0 ? r + b : r; |
| 23 | +} |
| 24 | + |
| 25 | +#ifdef _WIN32 |
| 26 | +//prevents displaying the black console window |
| 27 | +//on windows systems |
| 28 | +//depending on the toolchain (settings) this is also required |
| 29 | +#include <windows.h> |
| 30 | +int WINAPI WinMain (HINSTANCE hThisInstance, |
| 31 | + HINSTANCE hPrevInstance, |
| 32 | + LPSTR lpszArgument, |
| 33 | + int nCmdShow) |
| 34 | +#else |
| 35 | +int main(int argc, char** argv) |
| 36 | +#endif |
| 37 | +{ |
| 38 | +#ifdef _WIN32 |
| 39 | + LPSTR *argv = __argv; |
| 40 | + int argc = __argc; |
| 41 | + (void) argv; |
| 42 | + (void) argc; |
| 43 | +#endif |
| 44 | + SDL_Window *screen; |
| 45 | + SDL_Renderer *renderer; |
| 46 | + |
| 47 | + if ( SDL_Init(SDL_INIT_VIDEO) < 0 ){ |
| 48 | + fprintf(stderr, "Error: %s \n", SDL_GetError()); |
| 49 | + return 1; |
| 50 | + } |
| 51 | + |
| 52 | + SDL_SetHint( SDL_HINT_RENDER_DRIVER, getenv("RENDER_DRIVER") ); |
| 53 | + |
| 54 | + int width = 800; |
| 55 | + int height = 600; |
| 56 | + screen = SDL_CreateWindow("Caption", |
| 57 | + SDL_WINDOWPOS_CENTERED, |
| 58 | + SDL_WINDOWPOS_CENTERED, |
| 59 | + width, height, |
| 60 | + SDL_WINDOW_RESIZABLE); |
| 61 | + // SDL_WINDOW_FULLSCREEN_DESKTOP ); |
| 62 | + if ( screen == NULL ) { |
| 63 | + fprintf(stderr, "Error: %s \n", SDL_GetError()); |
| 64 | + return 2; |
| 65 | + } |
| 66 | + renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE ); |
| 67 | + //renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_SOFTWARE|SDL_RENDERER_TARGETTEXTURE ); |
| 68 | + if ( renderer == NULL ) { |
| 69 | + fprintf(stderr, "Error: %s \n", SDL_GetError()); |
| 70 | + return 3; |
| 71 | + } |
| 72 | + |
| 73 | + SDL_RenderSetLogicalSize(renderer, width, height); |
| 74 | + |
| 75 | + SDL_SetWindowTitle( screen, renderer->info.name ); |
| 76 | + |
| 77 | + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); |
| 78 | + SDL_RenderClear(renderer); |
| 79 | + |
| 80 | + |
| 81 | + int val = 3; |
| 82 | + SDL_shader_hint( SDL_D3D_PS_MAJOR_VERSION, &val ); |
| 83 | + val = 0; |
| 84 | + SDL_shader_hint( SDL_D3D_VS_MINOR_VERSION, &val ); |
| 85 | + |
| 86 | + SDL_Shader* shader = SDL_createShader( renderer, "../shaders/scale2x/scale2x" ); |
| 87 | + if ( shader == NULL ) { |
| 88 | + fprintf( stderr, "Error: %s \n", SDL_GetError() ); |
| 89 | + } |
| 90 | + SDL_assert(shader != NULL); |
| 91 | + |
| 92 | + SDL_Uniform* scaler = SDL_createUniform( shader, "scaler" ); |
| 93 | + SDL_assert(scaler); |
| 94 | + SDL_setUniform_i( scaler, 0 ); |
| 95 | + |
| 96 | + SDL_Texture *image; |
| 97 | + SDL_Texture *target_ld, *target_hd; |
| 98 | + SDL_Surface *srf; |
| 99 | + |
| 100 | + srf = IMG_Load( "../pixel_art.png" ); |
| 101 | + |
| 102 | + SDL_PixelFormat* fmt = SDL_AllocFormat( SDL_PIXELFORMAT_ARGB8888 ); |
| 103 | + SDL_assert(fmt != NULL); |
| 104 | + SDL_Surface* srf2 = SDL_ConvertSurface(srf, fmt, 0); |
| 105 | + SDL_assert(srf2 != NULL); |
| 106 | + |
| 107 | + if ( !srf ) { |
| 108 | + fprintf(stderr, "Error: %s \n", SDL_GetError()); |
| 109 | + return 5; |
| 110 | + } |
| 111 | + image = SDL_CreateTextureFromSurface(renderer, srf2 ); |
| 112 | + |
| 113 | + SDL_FreeSurface(srf); |
| 114 | + SDL_FreeSurface(srf2); |
| 115 | + |
| 116 | + |
| 117 | + target_ld = SDL_CreateTexture( renderer, SDL_PIXELFORMAT_ARGB8888, |
| 118 | + SDL_TEXTUREACCESS_TARGET, 400, 300 ); |
| 119 | + target_hd = SDL_CreateTexture( renderer, SDL_PIXELFORMAT_ARGB8888, |
| 120 | + SDL_TEXTUREACCESS_TARGET, 800, 600 ); |
| 121 | + |
| 122 | + SDL_SetRenderTarget( renderer, NULL ); |
| 123 | + SDL_Event e; |
| 124 | + SDL_SetRenderDrawColor(renderer, 0,0,0,255); |
| 125 | + int ret = 0; |
| 126 | + int quit = 0; |
| 127 | + while ( !quit ) { |
| 128 | + static int scaler_num=0; |
| 129 | + // Step 1: |
| 130 | + // Render something to the low-res texture |
| 131 | + SDL_SetRenderTarget( renderer, target_ld ); |
| 132 | + SDL_RenderCopy( renderer, image, NULL, NULL ); |
| 133 | + SDLTest_DrawString( renderer, 8, 8, scaler_names[scaler_num] ); |
| 134 | + // Step 2: |
| 135 | + // Copy low-res texture to the high-res texture, using scale shader |
| 136 | + SDL_SetRenderTarget( renderer, target_hd ); |
| 137 | + SDL_Rect dst = {0,0,800,600}; |
| 138 | + ret = SDL_renderCopyShd( shader, target_ld ,NULL,&dst); |
| 139 | + //ret = SDL_RenderCopy( renderer, target_ld, NULL, NULL ); |
| 140 | + if ( ret!=0 ){ |
| 141 | + fprintf(stderr,"Err: %s\n", SDL_GetError()); |
| 142 | + } |
| 143 | + // Step 3: |
| 144 | + // Render something to high-res texture |
| 145 | + |
| 146 | + // Step 4: |
| 147 | + // Copy the high-res texture to the screen |
| 148 | + SDL_SetRenderTarget( renderer, NULL ); |
| 149 | + //SDL_renderCopyShd( shader, target_hd, NULL, &dst ); |
| 150 | + SDL_RenderCopy( renderer, target_hd, NULL, NULL ); |
| 151 | + |
| 152 | + while (SDL_PollEvent(&e)){ |
| 153 | + switch ( e.type ) { |
| 154 | + case SDL_QUIT: |
| 155 | + quit = 1; |
| 156 | + break; |
| 157 | + case SDL_KEYDOWN: |
| 158 | + switch ( e.key.keysym.sym ) { |
| 159 | + case SDLK_SPACE: |
| 160 | + scaler_num = mod(scaler_num+1, 2); |
| 161 | + SDL_bindShader( shader ); |
| 162 | + SDL_setUniform_i( scaler, scaler_num ); |
| 163 | + break; |
| 164 | + case SDLK_TAB: |
| 165 | + scaler_num = mod(scaler_num-1, 2); |
| 166 | + SDL_bindShader( shader ); |
| 167 | + SDL_setUniform_i( scaler, scaler_num ); |
| 168 | + break; |
| 169 | + } |
| 170 | + break; |
| 171 | + //case SDL_WINDOWEVENT: |
| 172 | + // SDL_updateViewport( shader ); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + SDL_RenderPresent(renderer); |
| 177 | + |
| 178 | + SDL_SetRenderDrawColor(renderer, 160, 160, 160, 255); |
| 179 | + SDL_RenderClear(renderer); |
| 180 | + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | + SDL_destroyShader( shader ); |
| 186 | + SDL_DestroyTexture( image ); |
| 187 | + SDL_DestroyRenderer( renderer ); |
| 188 | + SDL_DestroyWindow( screen ); |
| 189 | + SDL_Quit(); |
| 190 | + return 0; |
| 191 | +} |
0 commit comments