commit a056aabe4a6d37de58465c84917645d744bc467c parent 67da3338107fbc36f7798e97daa761950f3ccd1f Author: Brian Swetland <swetland@frotz.net> Date: Sat, 9 Feb 2013 21:15:35 -0800 asset management - init_io() to determine path to executable - load files from $(BASEPATH)assets - update build to copy assets - move asset files into common/assets and hello/assets - remove SL files from VSE build Diffstat:
23 files changed, 144 insertions(+), 131 deletions(-)
diff --git a/common/app.h b/common/app.h @@ -1,6 +1,10 @@ +#ifndef _APP_H_ +#define _APP_H_ #if defined(USE_OPENGL) || !defined(_WIN32) #include "glapp.h" #else #include "dxapp.h" #endif + +#endif diff --git a/common/TextPS.glsl b/common/assets/TextPS.glsl diff --git a/common/TextPS.hlsl b/common/assets/TextPS.hlsl diff --git a/common/TextVS.glsl b/common/assets/TextVS.glsl diff --git a/common/TextVS.hlsl b/common/assets/TextVS.hlsl diff --git a/hello/font-vincent-8x8.png b/common/assets/font-vincent-8x8.png Binary files differ. diff --git a/hello/unitcube.obj b/common/assets/unitcube.obj diff --git a/hello/unitcubeoid.obj b/common/assets/unitcubeoid.obj diff --git a/hello/unitsphere.obj b/common/assets/unitsphere.obj diff --git a/common/dxapp.cc b/common/dxapp.cc @@ -98,7 +98,10 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l return 0; } +void init_io(void); + int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { + init_io(); app = createApp(); if (app->start(hInstance, nCmdShow)) return 0; @@ -675,40 +678,3 @@ int App::initBuffer(UniformBuffer *ub, void *data, int sz) { return _create_buffer(device, D3D10_BIND_CONSTANT_BUFFER, data, sz, &ub->buf); } -// ---- - -void printx(const char *fmt, ...) { -#if DEBUG || _DEBUG - char buf[128]; - va_list ap; - va_start(ap, fmt); - vsnprintf(buf, sizeof(buf), fmt, ap); - buf[127] = 0; - va_end(ap); - OutputDebugString(buf); -#endif -} - -void printmtx(float *m, const char *name) { -#if DEBUG || _DEBUG - printx("| %8.4f %8.4f %8.4f %8.4f | \"%s\"\n", m[0], m[1], m[2], m[3], name); - printx("| %8.4f %8.4f %8.4f %8.4f |\n", m[4], m[5], m[6], m[7]); - printx("| %8.4f %8.4f %8.4f %8.4f |\n", m[8], m[9], m[10], m[11]); - printx("| %8.4f %8.4f %8.4f %8.4f |\n", m[12], m[13], m[14], m[15]); -#endif -} - -int error(const char *fmt, ...) { - char buf[128]; - va_list ap; - va_start(ap, fmt); - vsnprintf(buf, sizeof(buf), fmt, ap); - va_end(ap); - buf[127] = 0; -#if _DEBUG - printx("ERROR: %s\n", buf); -#else - MessageBox(NULL, buf, "Error", MB_OK); -#endif - return -1; -} diff --git a/common/dxapp.h b/common/dxapp.h @@ -221,12 +221,8 @@ private: int active; }; -int compileShader(const char *fn, const char *profile, ID3D10Blob **shader); +//int compileShader(const char *fn, const char *profile, ID3D10Blob **shader); App *createApp(void); -void printx(const char *fmt, ...); -void printmtx(float *m, const char *name); -int error(const char *fmt, ...); - #endif diff --git a/common/glapp.cc b/common/glapp.cc @@ -48,12 +48,6 @@ void __check_gl_error(const char *fn, int line) { #define CHECK() __check_gl_error(__func__, __LINE__) #endif -static void die(const char *fmt, ...) { - printx("ERROR: %s\n", fmt); - exit(-1); -} - - #ifdef _WIN32 static int SDL_GL_ExtensionSupported(const char *name) { if (strstr((char*)glGetString(GL_EXTENSIONS), name)) @@ -176,50 +170,18 @@ App::App() : width(800), height(600), _vsync(1) { App::~App() { } +void init_io(void); + int main(int argc, char **argv) { + init_io(); App *app = createApp(); app->start(); app->release(); return 0; } - // ---- -static inline void ignore(int x) {} - -void printx(const char *fmt, ...) { - char buf[128]; - va_list ap; - va_start(ap, fmt); - vsnprintf(buf, sizeof(buf), fmt, ap); - buf[127] = 0; - va_end(ap); -#ifdef _WIN32 - OutputDebugString(buf); -#else - fwrite(buf, strlen(buf), 1, stderr); -#endif -} - -void printmtx(float *m, const char *name) { - printx("| %8.4f %8.4f %8.4f %8.4f | \"%s\"\n", m[0], m[1], m[2], m[3], name); - printx("| %8.4f %8.4f %8.4f %8.4f |\n", m[4], m[5], m[6], m[7]); - printx("| %8.4f %8.4f %8.4f %8.4f |\n", m[8], m[9], m[10], m[11]); - printx("| %8.4f %8.4f %8.4f %8.4f |\n", m[12], m[13], m[14], m[15]); -} - -int error(const char *fmt, ...) { - char buf[128]; - va_list ap; - va_start(ap, fmt); - vsnprintf(buf, sizeof(buf), fmt, ap); - va_end(ap); - buf[127] = 0; - printx("ERROR: %s\n", buf); - return -1; -} - #define OFF2PTR(off) (((char*) NULL) + off) void config_attr_info(GLAttrInfo *ai, unsigned count, int vidx, unsigned stride) { diff --git a/common/io.cc b/common/io.cc @@ -0,0 +1,83 @@ +/* Copyright 2013 Brian Swetland <swetland@frotz.net> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <stdarg.h> +#include <string.h> + +#include "util.h" + +extern const char *load_file_base_path; + +static char base_path[1024]; + +#ifdef _WIN32 +#include <windows.h> +void init_io(void) { + char *x; + GetModuleFileName(NULL, base_path, 1024); + base_path[1023] = 0; + x = strrchr(base_path, '\\'); + if (x) { + x[1] = 0; + strcat(base_path,"assets\\"); + load_file_base_path = base_path; + } +} +#else +void init_io(void) { +} +#endif + +void die(const char *fmt, ...) { + printx("ERROR: %s\n", fmt); + exit(-1); +} + +void printx(const char *fmt, ...) { + char buf[128]; + va_list ap; + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + buf[127] = 0; + va_end(ap); +#if defined(_WIN32) && defined(_DEBUG) + OutputDebugString(buf); +#else + fwrite(buf, strlen(buf), 1, stderr); +#endif +} + +void printmtx(float *m, const char *name) { + printx("| %8.4f %8.4f %8.4f %8.4f | \"%s\"\n", m[0], m[1], m[2], m[3], name); + printx("| %8.4f %8.4f %8.4f %8.4f |\n", m[4], m[5], m[6], m[7]); + printx("| %8.4f %8.4f %8.4f %8.4f |\n", m[8], m[9], m[10], m[11]); + printx("| %8.4f %8.4f %8.4f %8.4f |\n", m[12], m[13], m[14], m[15]); +} + +int error(const char *fmt, ...) { + char buf[128]; + va_list ap; + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + buf[127] = 0; +#if defined(_WIN32) && !defined(_DEBUG) + MessageBox(NULL, buf, "Error", MB_OK); +#else + printx("ERROR: %s\n", buf); +#endif + return -1; +} diff --git a/common/loadfile.cc b/common/loadfile.cc @@ -19,12 +19,19 @@ #include "util.h" +const char *load_file_base_path = ""; + void *load_file(const char *fn, unsigned *_sz) { + char tmp[1024]; void *data = 0; long sz; FILE *fp; - if(!(fp = fopen(fn, "rb"))) + snprintf(tmp, 1024, "%s%s", load_file_base_path, fn); + tmp[1023] = 0; + + printx("Loading '%s'...\n", tmp); + if(!(fp = fopen(tmp, "rb"))) goto exit; if (fseek(fp, 0, SEEK_END)) @@ -52,6 +59,6 @@ close_and_exit: fclose(fp); exit: if (!data) - fprintf(stderr, "failed to load '%s'\n", fn); + error("Failed to load '%s'", tmp); return data; } diff --git a/common/loadobj.cc b/common/loadobj.cc @@ -46,12 +46,18 @@ struct obj { vector<i3> triangles; }; +extern const char *load_file_base_path; + struct obj *load_obj(const char *fn) { + char tmp[1024]; char buf[128]; FILE *fp; struct obj *o = 0; - if (!(fp = fopen(fn, "r"))) + snprintf(tmp, 1024, "%s%s", load_file_base_path, fn); + tmp[1023] = 0; + printx("Loading Model '%s'...\n", tmp); + if (!(fp = fopen(tmp, "r"))) goto exit; o = new(obj); @@ -171,8 +177,10 @@ struct model *load_wavefront_obj(const char *fn) { struct obj *o; struct model *m; o = load_obj(fn); - if (!o) + if (!o) { + error("Failed to load Model '%s'", fn); return 0; + } m = obj_to_model(o); delete o; return m; diff --git a/common/loadpng.cc b/common/loadpng.cc @@ -21,15 +21,21 @@ #include "util.h" +extern const char *load_file_base_path; + void *_load_png(const char *fn, unsigned *_width, unsigned *_height, int ch, int inverty) { png_structp png; png_infop info; png_uint_32 w, h; int depth, ctype, itype, i; png_byte *data = 0; + char tmp[1024]; FILE *fp; - if ((fp = fopen(fn, "rb")) == NULL) + snprintf(tmp, 1024, "%s%s", load_file_base_path, fn); + tmp[1023] = 0; + + if ((fp = fopen(tmp, "rb")) == NULL) goto exit; if (!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) @@ -107,7 +113,7 @@ close_and_exit: fclose(fp); exit: if (!data) - fprintf(stderr,"failed to load '%s'\n", fn); + error("Failed to load '%s'", tmp); return data; } diff --git a/common/textgrid.cc b/common/textgrid.cc @@ -79,9 +79,9 @@ int TextGrid::init(App *a, int w, int h) { text_layout_desc, sizeof(text_layout_desc)/sizeof(text_layout_desc[0]))) return -1; #else - if (a->loadShader(&ps, "../common/TextPS."SL)) + if (a->loadShader(&ps, "TextPS."SL)) return -1; - if (a->loadShader(&vs, "../common/TextVS."SL, + if (a->loadShader(&vs, "TextVS."SL, text_layout_desc, sizeof(text_layout_desc)/sizeof(text_layout_desc[0]))) return -1; #endif diff --git a/common/util.h b/common/util.h @@ -41,5 +41,14 @@ float snoise(float x, float y); float snoise(float x, float y, float z); float snoise(float x, float y, float z, float w); +#ifdef _WIN32 +#define snprintf _snprintf +#endif + +void printx(const char *fmt, ...); +void printmtx(float *m, const char *name); +int error(const char *fmt, ...); +void die(const char *fmt, ...); + #endif diff --git a/hello/SimplePS.glsl b/hello/assets/SimplePS.glsl diff --git a/hello/SimplePS.hlsl b/hello/assets/SimplePS.hlsl diff --git a/hello/SimpleVS.glsl b/hello/assets/SimpleVS.glsl diff --git a/hello/SimpleVS.hlsl b/hello/assets/SimpleVS.hlsl diff --git a/hello/hello.vcxproj b/hello/hello.vcxproj @@ -79,6 +79,10 @@ <SubSystem>Windows</SubSystem> <AdditionalLibraryDirectories>../Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> </Link> + <PostBuildEvent> + <Command>XCOPY "$(ProjectDir)..\common\assets" "$(TargetDir)assets" /E /I /F /Y +XCOPY "$(ProjectDir)assets" "$(TargetDir)assets" /E /I /F /Y</Command> + </PostBuildEvent> </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='OpenGL-Debug|Win32'"> <ClCompile> @@ -95,7 +99,9 @@ <AdditionalLibraryDirectories>..\..\SDL-1.2.15\lib\x86;..\Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> </Link> <PostBuildEvent> - <Command>XCOPY "$(ProjectDir)..\..\SDL-1.2.15\lib\x86\SDL.dll" "$(TargetDir)" /E /I /F /Y</Command> + <Command>XCOPY "$(ProjectDir)..\..\SDL-1.2.15\lib\x86\SDL.dll" "$(TargetDir)" /E /I /F /Y +XCOPY "$(ProjectDir)..\common\assets" "$(TargetDir)assets" /E /I /F /Y +XCOPY "$(ProjectDir)assets" "$(TargetDir)assets" /E /I /F /Y</Command> </PostBuildEvent> </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> @@ -115,6 +121,10 @@ <EnableCOMDATFolding>true</EnableCOMDATFolding> <OptimizeReferences>true</OptimizeReferences> </Link> + <PostBuildEvent> + <Command>XCOPY "$(ProjectDir)..\common\assets" "$(TargetDir)assets" /E /I /F /Y +XCOPY "$(ProjectDir)assets" "$(TargetDir)assets" /E /I /F /Y</Command> + </PostBuildEvent> </ItemDefinitionGroup> <ItemGroup> <ClCompile Include="..\common\dxapp.cc"> @@ -124,6 +134,7 @@ <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='OpenGL-Debug|Win32'">false</ExcludedFromBuild> </ClCompile> + <ClCompile Include="..\common\io.cc" /> <ClCompile Include="..\common\loadfile.cc" /> <ClCompile Include="..\common\loadobj.cc" /> <ClCompile Include="..\common\loadpng.cc" /> @@ -142,45 +153,6 @@ <ClInclude Include="..\common\util.h" /> <ClInclude Include="..\common\textgrid.h" /> </ItemGroup> - <ItemGroup> - <FxCompile Include="SimplePS.hlsl"> - <FileType>Document</FileType> - <ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType> - <ShaderType Condition="'$(Configuration)|$(Platform)'=='OpenGL-Debug|Win32'">Pixel</ShaderType> - <ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">4.0</ShaderModel> - <ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Pixel</ShaderType> - </FxCompile> - <FxCompile Include="..\common\TextPS.hlsl"> - <ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType> - <ShaderType Condition="'$(Configuration)|$(Platform)'=='OpenGL-Debug|Win32'">Pixel</ShaderType> - <ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel> - <ShaderModel Condition="'$(Configuration)|$(Platform)'=='OpenGL-Debug|Win32'">4.0</ShaderModel> - <HeaderFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(OutDir)%(Filename).h</HeaderFileOutput> - <HeaderFileOutput Condition="'$(Configuration)|$(Platform)'=='OpenGL-Debug|Win32'">$(OutDir)%(Filename).h</HeaderFileOutput> - <VariableName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">codeTextPS</VariableName> - <VariableName Condition="'$(Configuration)|$(Platform)'=='OpenGL-Debug|Win32'">codeTextPS</VariableName> - </FxCompile> - <FxCompile Include="..\common\TextVS.hlsl"> - <ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType> - <ShaderType Condition="'$(Configuration)|$(Platform)'=='OpenGL-Debug|Win32'">Vertex</ShaderType> - <ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">4.0</ShaderModel> - <ShaderModel Condition="'$(Configuration)|$(Platform)'=='OpenGL-Debug|Win32'">4.0</ShaderModel> - <HeaderFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(OutDir)%(Filename).h</HeaderFileOutput> - <HeaderFileOutput Condition="'$(Configuration)|$(Platform)'=='OpenGL-Debug|Win32'">$(OutDir)%(Filename).h</HeaderFileOutput> - <VariableName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">codeTextVS</VariableName> - <VariableName Condition="'$(Configuration)|$(Platform)'=='OpenGL-Debug|Win32'">codeTextVS</VariableName> - <DisableOptimizations Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</DisableOptimizations> - <DisableOptimizations Condition="'$(Configuration)|$(Platform)'=='OpenGL-Debug|Win32'">false</DisableOptimizations> - </FxCompile> - </ItemGroup> - <ItemGroup> - <FxCompile Include="SimpleVS.hlsl"> - <ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType> - <ShaderType Condition="'$(Configuration)|$(Platform)'=='OpenGL-Debug|Win32'">Vertex</ShaderType> - <ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">4.0</ShaderModel> - <ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Vertex</ShaderType> - </FxCompile> - </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> </ImportGroup>