graphics

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit b2ab8de36424c9193f6eb812a1d8faa5ca1b916e
parent 1d3766dc8c4de90d641e0558389d22160b16ca74
Author: Brian Swetland <swetland@frotz.net>
Date:   Tue, 10 Sep 2013 08:59:52 -0700

clean up temporary string building, use stackstring instead of std::string

Diffstat:
MMakefile | 2+-
Mcommon/Effect.cc | 28+++++++++++++++-------------
Mcommon/Model.cc | 14+++++++-------
Mcommon/io.cc | 47++++++++++++++++++++++++++---------------------
Acommon/io.h | 29+++++++++++++++++++++++++++++
Mcommon/loadfile.cc | 3++-
Mcommon/loadobj.cc | 3++-
Mcommon/loadpng.cc | 3++-
Acommon/stringutils.h | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcommon/util.h | 4----
Mtest/module.mk | 6++++++
Mtools/mkfont.cc | 2++
12 files changed, 183 insertions(+), 49 deletions(-)

diff --git a/Makefile b/Makefile @@ -22,7 +22,7 @@ SDLFLAGS := $(shell $(SDLCFG) --cflags) #SDLLIBS := $(shell $(SDLCFG) --static-libs) SDLLIBS := /work/sdl2/lib/libSDL2.a -lpthread -lrt -ldl -HOST_CFLAGS := $(SDLFLAGS) -Wall -g -O2 +HOST_CFLAGS := $(SDLFLAGS) -Wall -g -O0 HOST_CFLAGS += -std=c++0x HOST_CFLAGS += -Icommon HOST_CFLAGS += -Ithirdparty/freetype-2.5.0.1/include diff --git a/common/Effect.cc b/common/Effect.cc @@ -15,6 +15,8 @@ #include <string> +#include "stringutils.h" + #include "Effect.h" void Effect::apply(void) { @@ -24,12 +26,10 @@ void Effect::apply(void) { int Effect::init(const char *name) { const char *x = strchr(name, '+'); if (x) { - std::string vname(name, x - name); - std::string fname(name, x - name); - vname.append(".vertex"); - fname.append(".fragment"); + int len = x - name; + string128 sname(stringptr(name, len)); + string1024 defines; x++; - std::string defines; while (*x) { const char *n = x; x = strchr(n, '+'); @@ -44,18 +44,20 @@ int Effect::init(const char *name) { break; } } - if (vs.load(vname.c_str(), defines.c_str())) + sname.append(".vertex"); + if (vs.load(sname, defines)) return -1; - if (ps.load(fname.c_str(), defines.c_str())) + sname.trim(len).append(".fragment"); + if (ps.load(sname, defines)) return -1; } else { - std::string vname(name); - std::string fname(name); - vname.append(".vertex"); - fname.append(".fragment"); - if (vs.load(vname.c_str())) + string128 sname(name); + int len = sname.length(); + sname.append(".vertex"); + if (vs.load(sname)) return -1; - if (ps.load(fname.c_str())) + sname.trim(len).append(".fragment"); + if (ps.load(sname)) return -1; } if (pgm.link(&vs, &ps)) diff --git a/common/Model.cc b/common/Model.cc @@ -17,6 +17,7 @@ #include "Model.h" +#include "stringutils.h" #include "util.h" // idx, src, dst, count, offset, stride, divisor @@ -33,23 +34,22 @@ void Model::render(void) { } int Model::init(const char *name) { + string128 fname(name); + int len = fname.length(); struct model *m; VertexBuffer *data[] = { &vbuf, &vbuf, &vbuf, }; - std::string mname(name); - std::string tname(name); - mname.append(".obj"); - tname.append(".png"); - - if (!(m = load_wavefront_obj(mname.c_str()))) + fname.append(".obj"); + if (!(m = load_wavefront_obj(fname))) return error("cannot load model"); printx("Object '%s' loaded. %d vertices, %d indices.\n", name, m->vcount, m->icount); - texture.load(tname.c_str(), OPT_TEX2D_GEN_MIPMAP); + fname.trim(len).append(".png"); + texture.load(fname, OPT_TEX2D_GEN_MIPMAP); vbuf.load(m->vdata, 32 * m->vcount); ibuf.load(m->idx, 2 * m->icount); diff --git a/common/io.cc b/common/io.cc @@ -20,15 +20,16 @@ #include <sys/stat.h> #include "util.h" +#include "io.h" -static char base_path[1024 + 8] = ""; +static char _base_path[1024 + 8] = ""; +static stringptr base_path; static const char *defaultsearch[] = { #ifdef _WIN32 "assets\\", #else "assets/", - "../common/assets/", #endif NULL, }; @@ -41,20 +42,21 @@ static const char *nosearch[] = { static const char **search = defaultsearch; void io_ignore_asset_paths(void) { - base_path[0] = 0; + _base_path[0] = 0; + base_path = stringptr(_base_path); search = nosearch; } -FILE *fopen_asset(const char *fn, const char *kind) { - char path[2048 + 64]; + +FILE *io_fopen_asset(stringptr fn, const char *kind) { + stackstring<4096> path(base_path); + int len = path.length(); FILE *fp; int i; - if (strlen(fn) > 1024) - return NULL; for (i = 0; search[i]; i++) { - sprintf(path, "%s%s%s", base_path, search[i], fn); + path.trim(len).append(search[i]).append(fn); fp = fopen(path, "rb"); if (fp != NULL) { - printx("Loading %s from '%s'...\n", kind, path); + printx("Loading %s from '%s'...\n", kind, path.cstr()); return fp; } } @@ -66,40 +68,43 @@ FILE *fopen_asset(const char *fn, const char *kind) { #include <windows.h> void init_io(void) { char *x; - GetModuleFileName(NULL, base_path, 1024); - base_path[1023] = 0; - x = strrchr(base_path, '\\'); + GetModuleFileName(NULL, _base_path, 1024); + _base_path[1023] = 0; + x = strrchr(_base_path, '\\'); if (x) x[1] = 0; else - strcpy(base_path,".\\"); + strcpy(_base_path,".\\"); + base_path = stringptr(_basepath); } #else #include <unistd.h> void init_io(void) { char *x; int r; - r = readlink("/proc/self/exe", base_path, 1024); + r = readlink("/proc/self/exe", _base_path, 1024); if (r < 0) return; - x = strrchr(base_path, '/'); + x = strrchr(_base_path, '/'); if (x) x[1] = 0; else - strcpy(base_path,"./"); + strcpy(_base_path,"./"); + base_path = stringptr(_base_path); } #endif -int file_get_mtime(const char *fn) { +int file_get_mtime(stringptr fn) { struct stat s; - char buf[2048 + 64]; - snprintf(buf, 1024, "%s%s%s", base_path, search[0], fn); - if (stat(buf, &s)) + stackstring<4096> path(base_path); + path.append(search[0]).append(fn); + if (path.error()) + return -1; + if (stat(path, &s)) return -1; return s.st_mtime; } - void vprintx(const char *fmt, va_list ap) { char buf[128]; vsnprintf(buf, sizeof(buf), fmt, ap); diff --git a/common/io.h b/common/io.h @@ -0,0 +1,29 @@ +/* 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. + */ + +#ifndef _IO_H_ +#define _IO_H_ + +#include <stringutils.h> +#include <stdio.h> + +/* treat filenames as normal filenames, don't use asset search process */ +void io_ignore_asset_paths(void); + +/* locate an asset and return a FILE* */ +FILE *io_fopen_asset(stringptr fn, const char *kind); + +#endif + diff --git a/common/loadfile.cc b/common/loadfile.cc @@ -18,13 +18,14 @@ #include <string.h> #include "util.h" +#include "io.h" void *load_file(const char *fn, unsigned *_sz) { void *data = 0; long sz; FILE *fp; - if (!(fp = fopen_asset(fn, "file"))) + if (!(fp = io_fopen_asset(fn, "file"))) goto exit; if (fseek(fp, 0, SEEK_END)) diff --git a/common/loadobj.cc b/common/loadobj.cc @@ -21,6 +21,7 @@ using std::vector; #include "util.h" +#include "io.h" struct v3 { float x; @@ -51,7 +52,7 @@ struct obj *load_obj(const char *fn) { FILE *fp; struct obj *o = 0; - if (!(fp = fopen_asset(fn, "model"))) + if (!(fp = io_fopen_asset(fn, "model"))) goto exit; o = new(obj); diff --git a/common/loadpng.cc b/common/loadpng.cc @@ -20,6 +20,7 @@ #include <png.h> #include "util.h" +#include "io.h" void *_load_png(const char *fn, unsigned *_width, unsigned *_height, int options) { png_structp png; @@ -31,7 +32,7 @@ void *_load_png(const char *fn, unsigned *_width, unsigned *_height, int options ch = (options & OPT_PNG_GRAY) ? 1 : 4; - if ((fp = fopen_asset(fn, "image")) == NULL) + if ((fp = io_fopen_asset(fn, "image")) == NULL) goto exit; if (!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) diff --git a/common/stringutils.h b/common/stringutils.h @@ -0,0 +1,91 @@ +/* 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. + */ + +#ifndef _STRING_UTILS_H_ +#define _STRING_UTILS_H_ + +#include <string.h> + +class stringptr { +public: + stringptr() : str(""), len(0) {} + stringptr(const char *s) : str(s), len(strlen(s)) {} + stringptr(const char *s, int _len) : str(s), len(_len) {} + int length() { return len; } + const char *cstr(void) { return str; } + operator const char *() { return str; } +private: + const char *str; + int len; +}; + +template <int N> +class stackstring { +public: + stackstring() { + len = 0; + oops = 0; + str[0] = 0; + } + stackstring(stringptr sp) { + int l2 = sp.length(); + if (l2 >= N) { + len = N - 1; + oops = 1; + } else { + len = l2; + oops = 0; + } + memcpy(str, sp.cstr(), len + 1); + } + stackstring<N>& trim(int n) { + if (len > n) { + len = n; + str[len] = 0; + } + return *this; + } + stackstring<N>& append(const char *astr, int alen) { + if ((N - len - 1) >= alen) { + memcpy(str + len, astr, alen + 1); + len += alen; + } else { + memcpy(str + len, astr, N - len - 1); + len = N - 1; + oops = 1; + } + return *this; + } + stackstring<N>& append(stringptr sp) { + return append(sp.cstr(), sp.length()); + } + stackstring<N>& append(char c) { + return append(&c, 1); + } + int length(void) { return len; } + int error(void) { return oops; } + const char *cstr(void) { return str; } + operator const char *() { return str; } + operator stringptr () { return stringptr(str, len); } +private: + int len; + int oops; + char str[N]; +}; + +typedef stackstring<1024> string1024; +typedef stackstring<128> string128; + +#endif diff --git a/common/util.h b/common/util.h @@ -30,9 +30,6 @@ int file_get_mtime(const char *fn); int save_png_rgba(const char *fn, void *data, unsigned width, unsigned height); int save_png_gray(const char *fn, void *data, unsigned width, unsigned height); -/* tread filenames as normal filenames, don't use asset search process */ -void io_ignore_asset_paths(void); - /* model helpers */ struct model { @@ -60,6 +57,5 @@ void printmtx(float *m, const char *name); int error(const char *fmt, ...); void die(const char *fmt, ...); -FILE *fopen_asset(const char *fn, const char *kind); #endif diff --git a/test/module.mk b/test/module.mk @@ -5,3 +5,9 @@ M_OBJS := object.o M_LIBS := common include build/app.mk + +M_NAME := menu +M_OBJS := menu.o +M_LIBS := common + +include build/app.mk diff --git a/tools/mkfont.cc b/tools/mkfont.cc @@ -20,6 +20,8 @@ #include FT_GLYPH_H #include <util.h> +#include <io.h> + #include <texturefont.h> #include <algorithm>