|
| 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 | +int mod(int a, int b) |
| 15 | +{ |
| 16 | + int r = a % b; |
| 17 | + return r < 0 ? r + b : r; |
| 18 | +} |
| 19 | + |
| 20 | +#ifdef _WIN32 |
| 21 | +//prevents displaying the black console window |
| 22 | +//on windows systems |
| 23 | +//depending on the toolchain (settings) this is also required |
| 24 | +#include <windows.h> |
| 25 | +int WINAPI WinMain (HINSTANCE hThisInstance, |
| 26 | + HINSTANCE hPrevInstance, |
| 27 | + LPSTR lpszArgument, |
| 28 | + int nCmdShow) |
| 29 | +#else |
| 30 | +int main(int argc, char** argv) |
| 31 | +#endif |
| 32 | +{ |
| 33 | +#ifdef _WIN32 |
| 34 | + LPSTR *argv = __argv; |
| 35 | + int argc = __argc; |
| 36 | + (void) argv; |
| 37 | + (void) argc; |
| 38 | +#endif |
| 39 | + SDL_Window *screen; |
| 40 | + SDL_Renderer *renderer; |
| 41 | + |
| 42 | + if ( SDL_Init(SDL_INIT_VIDEO) < 0 ){ |
| 43 | + fprintf(stderr, "Error: %s \n", SDL_GetError()); |
| 44 | + return 1; |
| 45 | + } |
| 46 | + |
| 47 | + SDL_SetHint( SDL_HINT_RENDER_DRIVER, getenv("RENDER_DRIVER") ); |
| 48 | + |
| 49 | + int width = 800; |
| 50 | + int height = 600; |
| 51 | + screen = SDL_CreateWindow("Caption", |
| 52 | + SDL_WINDOWPOS_CENTERED, |
| 53 | + SDL_WINDOWPOS_CENTERED, |
| 54 | + width, height, |
| 55 | + SDL_WINDOW_RESIZABLE); |
| 56 | + // SDL_WINDOW_FULLSCREEN_DESKTOP ); |
| 57 | + if ( screen == NULL ) { |
| 58 | + fprintf(stderr, "Error: %s \n", SDL_GetError()); |
| 59 | + return 2; |
| 60 | + } |
| 61 | + renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_ACCELERATED|SDL_RENDERER_TARGETTEXTURE ); |
| 62 | + //renderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_SOFTWARE|SDL_RENDERER_TARGETTEXTURE ); |
| 63 | + if ( renderer == NULL ) { |
| 64 | + fprintf(stderr, "Error: %s \n", SDL_GetError()); |
| 65 | + return 3; |
| 66 | + } |
| 67 | + |
| 68 | + SDL_RenderSetLogicalSize(renderer, width, height); |
| 69 | + |
| 70 | + SDL_SetWindowTitle( screen, renderer->info.name ); |
| 71 | + |
| 72 | + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); |
| 73 | + SDL_RenderClear(renderer); |
| 74 | + |
| 75 | + SDL_shader_hint(SDL_GL_TEX0_NAME,"u_texture" ); |
| 76 | + SDL_shader_hint(SDL_GL_TEX1_NAME,"u_normals" ); |
| 77 | + SDL_Shader* shader = SDL_createShader( renderer, "../shaders/normal_map/normal_map" ); |
| 78 | + |
| 79 | + if ( shader == NULL ) { |
| 80 | + fprintf( stderr, "Error: %s \n", SDL_GetError() ); |
| 81 | + } |
| 82 | + SDL_assert(shader != NULL); |
| 83 | + |
| 84 | + SDL_Uniform* resolution = SDL_createUniform( shader, "Resolution" ); |
| 85 | + SDL_Uniform* lightPos = SDL_createUniform( shader, "LightPos" ); |
| 86 | + SDL_Uniform* lightColor = SDL_createUniform( shader, "LightColor" ); |
| 87 | + SDL_Uniform* ambientColor = SDL_createUniform( shader, "AmbientColor" ); |
| 88 | + SDL_Uniform* falloff = SDL_createUniform( shader, "Falloff" ); |
| 89 | + |
| 90 | + SDL_assert(resolution != NULL); |
| 91 | + SDL_assert(lightPos != NULL); |
| 92 | + SDL_assert(lightColor != NULL); |
| 93 | + SDL_assert(ambientColor != NULL); |
| 94 | + SDL_assert(falloff != NULL); |
| 95 | + |
| 96 | + SDL_setUniform_f2( resolution, 800, 600 ); |
| 97 | + SDL_setUniform_f3( lightPos, 0.5,0.5,0.075f ); |
| 98 | + SDL_setUniform_f4( lightColor, 1, 0.7f, 0.7f, 7 ); |
| 99 | + SDL_setUniform_f4( ambientColor, 1, 1, 1, 0.3f ); |
| 100 | + SDL_setUniform_f3( falloff, 0.4f, 3, 20 ); |
| 101 | + |
| 102 | + SDL_Texture* tex[2]; |
| 103 | + SDL_Surface* srf; |
| 104 | + char* names[] = {"../stone.png", "../stone_n.png"}; |
| 105 | + for( int i=0; i<2; i++ ){ |
| 106 | + srf = IMG_Load( names[i] ); |
| 107 | + |
| 108 | + SDL_PixelFormat* fmt = SDL_AllocFormat( SDL_PIXELFORMAT_ARGB8888 ); |
| 109 | + SDL_assert(fmt != NULL); |
| 110 | + SDL_Surface* srf2 = SDL_ConvertSurface(srf, fmt, 0); |
| 111 | + SDL_assert(srf2 != NULL); |
| 112 | + |
| 113 | + if ( !srf ) { |
| 114 | + fprintf(stderr, "Error: %s \n", SDL_GetError()); |
| 115 | + return 5; |
| 116 | + } |
| 117 | + tex[i] = SDL_CreateTextureFromSurface(renderer, srf2 ); |
| 118 | + |
| 119 | + SDL_FreeSurface(srf); |
| 120 | + SDL_FreeSurface(srf2); |
| 121 | + } |
| 122 | + |
| 123 | + SDL_SetRenderTarget( renderer, NULL ); |
| 124 | + SDL_Event e; |
| 125 | + SDL_SetRenderDrawColor(renderer, 0,0,0,1); |
| 126 | + int ret = 0; |
| 127 | + int quit = 0; |
| 128 | + while ( !quit ) { |
| 129 | + //SDLTest_DrawString( renderer, 8, 8, shader_names[current_shader] ); |
| 130 | + SDL_Rect dst = {0,0,800,600}; |
| 131 | + ret = SDL_renderCopyShdArray( shader, tex,NULL,&dst,2 ); |
| 132 | + if ( ret!=0 ){ |
| 133 | + fprintf(stderr,"Err: %s\n", SDL_GetError()); |
| 134 | + } |
| 135 | + |
| 136 | + while (SDL_PollEvent(&e)){ |
| 137 | + switch ( e.type ) { |
| 138 | + case SDL_QUIT: |
| 139 | + quit = 1; |
| 140 | + break; |
| 141 | + case SDL_KEYDOWN: |
| 142 | + switch ( e.key.keysym.sym ) { |
| 143 | + case SDLK_SPACE: |
| 144 | + break; |
| 145 | + case SDLK_TAB: |
| 146 | + break; |
| 147 | + } |
| 148 | + break; |
| 149 | + case SDL_MOUSEMOTION: |
| 150 | + shader->bindShader( shader ); |
| 151 | + SDL_setUniform_f3( lightPos, |
| 152 | + (float) e.motion.x/800.0f, |
| 153 | + (float) 1.0f-e.motion.y/600.0f, |
| 154 | + 0.450f ); |
| 155 | + break; |
| 156 | + //case SDL_WINDOWEVENT: |
| 157 | + // SDL_updateViewport( shader ); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + SDL_RenderPresent(renderer); |
| 162 | + |
| 163 | + SDL_SetRenderDrawColor(renderer, 160, 160, 160, 255); |
| 164 | + SDL_RenderClear(renderer); |
| 165 | + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + SDL_destroyUniform(shader, resolution); |
| 171 | + SDL_destroyUniform(shader, lightPos); |
| 172 | + SDL_destroyUniform(shader, lightColor); |
| 173 | + SDL_destroyUniform(shader, ambientColor); |
| 174 | + SDL_destroyUniform(shader, falloff); |
| 175 | + |
| 176 | + SDL_destroyShader( shader ); |
| 177 | + SDL_DestroyTexture( tex[0] ); |
| 178 | + SDL_DestroyTexture( tex[1] ); |
| 179 | + SDL_DestroyRenderer( renderer ); |
| 180 | + SDL_DestroyWindow( screen ); |
| 181 | + SDL_Quit(); |
| 182 | + return 0; |
| 183 | +} |
0 commit comments