Skip to content

Commit

Permalink
Using one function-prefix per header
Browse files Browse the repository at this point in the history
All functions are prefixed by the name of their header now to improve
code-readability.
If C only had namespaces :/
  • Loading branch information
lnqs committed Oct 30, 2013
1 parent b4c4511 commit 9b41f04
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
*.o
*.glsl.i
shader_code.h
textrender_glyphs.h
glyphs.h
planeshift.elf
planeshift
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ SHADERS = $(wildcard *.glsl)
SHADERS_PREPROCESSED = $(SHADERS:.glsl=.glsl.i)
SHADER_HEADER = shader_code.h
GLYPHS_IMAGE = glyphs.png
GLYPHS_HEADER = textrender_glyphs.h
GLYPHS_HEADER = glyphs.h
EXECUTABLE = planeshift.elf
COMPRESSED = planeshift

Expand Down
6 changes: 3 additions & 3 deletions clib.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
// optimal than jumping to these functions implemented in pure asm in another
// object.

static stdcall void exit_(int code)
static stdcall void clib_exit(int code)
{
__asm__ volatile ("int $0x80"
:
: "a" (SYS_exit_group),
"b" (code));
}

static stdcall void clone_(int (*fn)(void*), void* stack, int flags, void* data)
static stdcall void clib_clone(int (*fn)(void*), void* stack, int flags, void* data)
{
__asm__ volatile ("subl $4,%2\n"
"movl %4,(%2)\n"
Expand All @@ -44,7 +44,7 @@ static stdcall void clone_(int (*fn)(void*), void* stack, int flags, void* data)
"r" (SYS_exit));
}

static stdcall void inaccurate_memcpy(void* dest, const void* src, size_t n)
static stdcall void clib_inaccurate_memcpy(void* dest, const void* src, size_t n)
{
// To save some instructions, only full words are copied, rest is ignored.
// Therefore inaccurate -- while this is perfectly fine for us. This
Expand Down
13 changes: 7 additions & 6 deletions gl_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// It get's funny here! This string is written to the elf-header by screw_elf_header.py,
// while the symbol for it is defined in linker.ld
extern const char _gl_library;
extern const char gl_functions_library;

// the members of this struct have to have the same order as in the hashes-array!
static struct
Expand All @@ -31,9 +31,9 @@ static struct
void GLAPIENTRY (*glUniformMatrix3fv)(GLint, GLsizei, GLboolean, const GLfloat*);
GLint GLAPIENTRY (*glGetAttribLocation)(GLuint, const GLchar*);
void GLAPIENTRY (*glVertexAttrib2f)(GLuint, GLfloat, GLfloat);
} gl;
} gl_functions;

static const gnu_hash_t gl_hashes[] = {
static const gnu_hash_t gl_functions_hashes[] = {
0xfd3eaa9d, // glBegin
0x0f83490f, // glEnd
0x835cdd03, // glCreateShader
Expand All @@ -56,11 +56,12 @@ static const gnu_hash_t gl_hashes[] = {
0x233f3994 // glVertexAttrib2f
};

static stdcall void initialize_gl_functions()
static stdcall void gl_functions_initialize()
{
for (int i = 0; i < sizeof(gl_hashes) / sizeof(gnu_hash_t); i++)
for (int i = 0; i < sizeof(gl_functions_hashes) / sizeof(gnu_hash_t); i++)
{
((void**)&gl)[i] = resolve_symbol(&_gl_library, gl_hashes[i]);
((void**)&gl_functions)[i] =
linker_lookup_symbol(&gl_functions_library, gl_functions_hashes[i]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion keypoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct keypoint
struct scene_state state;
} packed;

static const struct keypoint keypoints[] = {
static const struct keypoint keypoint_points[] = {
{
.time = 0,
.state = {
Expand Down
22 changes: 11 additions & 11 deletions linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ extern const struct link_map* _link_map; // Provided by linker.ld linker-script
// We're using the string from dynsym. Symbol is provided by linker-script
extern const char _libc_filename;

static const gnu_hash_t libc_dlopen_mode_hash = 0xf2cb98a2;
static const gnu_hash_t linker_dlopen_mode_hash = 0xf2cb98a2;

// This is the hash-function used for gnu-style hash-tables in elf.
// While it doesn't really matter what function we use -- we don't use the
// hash-tables at all -- it should be fine
static stdcall gnu_hash_t gnu_hash(const char* s)
static stdcall gnu_hash_t linker_gnu_hash(const char* s)
{
gnu_hash_t h = 5381;

Expand All @@ -30,7 +30,7 @@ static stdcall gnu_hash_t gnu_hash(const char* s)
return h & 0xffffffff;
}

static stdcall const struct link_map* link_map_entry_for_library(const char* library)
static stdcall const struct link_map* linker_map_for_library(const char* library)
{
// assume the entry exists to save some bytes
const struct link_map* map = _link_map;
Expand All @@ -45,7 +45,7 @@ static stdcall const struct link_map* link_map_entry_for_library(const char* lib
return map;
}

static stdcall const void* get_table(const struct link_map* map, int type)
static stdcall const void* linker_get_table(const struct link_map* map, int type)
{
const ElfW(Dyn)* dyn = (const ElfW(Dyn)*)map->l_ld;
while (dyn->d_tag != type)
Expand All @@ -61,29 +61,29 @@ static stdcall const void* get_table(const struct link_map* map, int type)
// NVidias GL implementation is the one that made problems on my system.
// Therefore, to avoid the size-overhead of implementing both, we keep it way
// simpler here and just walk the symbol table to find symbols.
static stdcall void* resolve_symbol(const char* library, gnu_hash_t hash)
static stdcall void* linker_lookup_symbol(const char* library, gnu_hash_t hash)
{
// To keep the code short, we trust in the library to be loaded and finding
// the symbol.
// If this isn't the case, it'll behave undefined and segfault eventually.

const struct link_map* map = (const struct link_map*)link_map_entry_for_library(library);
const ElfW(Sym)* symtab = (const ElfW(Sym)*)get_table(map, DT_SYMTAB);
const char* strtab = (const char*)get_table(map, DT_STRTAB);
const struct link_map* map = (const struct link_map*)linker_map_for_library(library);
const ElfW(Sym)* symtab = (const ElfW(Sym)*)linker_get_table(map, DT_SYMTAB);
const char* strtab = (const char*)linker_get_table(map, DT_STRTAB);

while (hash != gnu_hash(strtab + symtab->st_name))
while (hash != linker_gnu_hash(strtab + symtab->st_name))
{
symtab += 1;
}

return (void*)(map->l_addr + symtab->st_value);
}

static stdcall void load_library(const char* filename)
static stdcall void linker_load_library(const char* filename)
{
// Yay! Using internals of libc! Let's just cross fingers this won't change too fast :/
void* (*__libc_dlopen_mode_fn)(const char*, int)
= (void* (*)(const char*, int))resolve_symbol(&_libc_filename, libc_dlopen_mode_hash);
= (void* (*)(const char*, int))linker_lookup_symbol(&_libc_filename, linker_dlopen_mode_hash);
__libc_dlopen_mode_fn(filename, RTLD_NOW | RTLD_GLOBAL);
}

Expand Down
2 changes: 1 addition & 1 deletion linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SECTIONS
/* Well... this is reliable as long as libc is the only lib we're linking to directly */
PROVIDE (_libc_filename = LOADADDR(.dynstr) + 1);
/* Yay! screw_elf_header.py writes us strings to the elf-header, define symbols for them here! */
PROVIDE (_gl_library = __executable_start + 46);
PROVIDE (gl_functions_library = __executable_start + 46);

. = SEGMENT_START("text-segment", 0x08048000) + SIZEOF_HEADERS;

Expand Down
104 changes: 52 additions & 52 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,43 @@ struct effect_parameters
} packed;

static struct scene_state scene_state;
static const struct keypoint* keypoint = keypoints;
static const struct keypoint* keypoint = keypoint_points;
static struct effect_parameters effect_parameters;

static GLuint overlay_texture;

static stdcall void initialize_sdl()
static stdcall void setup_window()
{
sdl.SDL_SetVideoMode(RESOLUTION_X, RESOLUTION_Y, 0,
sdl_functions.SDL_SetVideoMode(RESOLUTION_X, RESOLUTION_Y, 0,
SDL_OPENGL | (FULLSCREEN ? SDL_FULLSCREEN : 0));
sdl.SDL_ShowCursor(SDL_DISABLE);
sdl_functions.SDL_ShowCursor(SDL_DISABLE);
}

static stdcall void cleanup_sdl()
static stdcall void cleanup()
{
// SDL_Quit crashes since main() is removed, but we need this call to reset
// the screen resolution when running fullscreen
sdl.SDL_QuitSubSystem(SDL_INIT_VIDEO);
sdl_functions.SDL_QuitSubSystem(SDL_INIT_VIDEO);
}

static stdcall void create_overlay_texture(GLuint program)
{
gl.glActiveTexture(GL_TEXTURE0);
gl.glBindTexture(GL_TEXTURE_2D, overlay_texture);
gl.glGenTextures(1, &overlay_texture);
gl_functions.glActiveTexture(GL_TEXTURE0);
gl_functions.glBindTexture(GL_TEXTURE_2D, overlay_texture);
gl_functions.glGenTextures(1, &overlay_texture);

uniform_int(program, uniform(uf_text_texture), 0);
shader_uniform_int(program, uniform(uf_text_texture), 0);

gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gl_functions.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl_functions.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl_functions.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gl_functions.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}

static stdcall bool exit_requested()
{
SDL_Event event;
sdl.SDL_PollEvent(&event);
sdl_functions.SDL_PollEvent(&event);

if (event.type == SDL_QUIT)
{
Expand All @@ -84,10 +84,10 @@ static stdcall bool update_scene()
// a new mechanism to read them from the keypoints, or a lot of additionals
// points, both leading to way too much code.

uint32_t time = sdl.SDL_GetTicks();
uint32_t time = sdl_functions.SDL_GetTicks();
const struct keypoint* next = keypoint + 1;

if (time > keypoints[sizeof(keypoints) / sizeof(struct keypoint) - 1].time)
if (time > keypoint_points[sizeof(keypoint_points) / sizeof(struct keypoint) - 1].time)
{
return false;
}
Expand Down Expand Up @@ -119,62 +119,62 @@ static stdcall bool update_scene()

static stdcall void mainloop(GLuint program)
{
GLuint position_location = gl.glGetAttribLocation(program, uniform(in_position));
GLuint texcoord_location = gl.glGetAttribLocation(program, uniform(in_texcoord));
GLuint position_location = gl_functions.glGetAttribLocation(program, uniform(in_position));
GLuint texcoord_location = gl_functions.glGetAttribLocation(program, uniform(in_texcoord));

while (!exit_requested() && update_scene())
{
uniform_vector3(program, uniform(uf_cam_position), scene_state.position);
uniform_matrix3(program, uniform(uf_cam_orientation), scene_state.orientation);
shader_uniform_vector3(program, uniform(uf_cam_position), scene_state.position);
shader_uniform_matrix3(program, uniform(uf_cam_orientation), scene_state.orientation);
// Since the three parameters follow each other in the struct,
// we just treat them as vector to save some bytes.
// It looks the same in memory anyway.
uniform_vector3(program, uniform(uf_fractal_params), (float*)&scene_state.box_scale);
uniform_vector3(program, uniform(uf_effect_params), (float*)&effect_parameters);

gl.glBegin(GL_QUADS);
gl.glVertexAttrib2f(position_location, -WINDOW_RATIO, -1.0);
gl.glVertexAttrib2f(texcoord_location, 1.0, 1.0);
gl.glVertexAttrib2f(position_location, WINDOW_RATIO, -1.0);
gl.glVertexAttrib2f(texcoord_location, 1.0, 0.0);
gl.glVertexAttrib2f(position_location, WINDOW_RATIO, 1.0);
gl.glVertexAttrib2f(texcoord_location, 0.0, 0.0);
gl.glVertexAttrib2f(position_location, -WINDOW_RATIO, 1.0);
gl.glVertexAttrib2f(texcoord_location, 0.0, 1.0);
gl.glEnd();

gl.glUseProgram(program);

sdl.SDL_GL_SwapBuffers();
shader_uniform_vector3(program, uniform(uf_fractal_params), (float*)&scene_state.box_scale);
shader_uniform_vector3(program, uniform(uf_effect_params), (float*)&effect_parameters);

gl_functions.glBegin(GL_QUADS);
gl_functions.glVertexAttrib2f(position_location, -WINDOW_RATIO, -1.0);
gl_functions.glVertexAttrib2f(texcoord_location, 1.0, 1.0);
gl_functions.glVertexAttrib2f(position_location, WINDOW_RATIO, -1.0);
gl_functions.glVertexAttrib2f(texcoord_location, 1.0, 0.0);
gl_functions.glVertexAttrib2f(position_location, WINDOW_RATIO, 1.0);
gl_functions.glVertexAttrib2f(texcoord_location, 0.0, 0.0);
gl_functions.glVertexAttrib2f(position_location, -WINDOW_RATIO, 1.0);
gl_functions.glVertexAttrib2f(texcoord_location, 0.0, 1.0);
gl_functions.glEnd();

gl_functions.glUseProgram(program);

sdl_functions.SDL_GL_SwapBuffers();
}
}

// Save overhead from crt1.o, get control from entry point on
void _start()
{
initialize_sdl_functions();
initialize_sdl();
initialize_gl_functions();
initialize_sound();
sdl_functions_initialize();
setup_window();
gl_functions_initialize();
sound_initialize();

GLuint program = compile_program(vertex_glsl, fragment_glsl);
GLuint program = shader_compile_program(vertex_glsl, fragment_glsl);
create_overlay_texture(program);

/////// TODO: Testcode, remove
set_text(_("int main(int argc, char** argv)\n"
"{\n"
" printf(\"Hello World!\");\n"
"}"));
gl.glActiveTexture(GL_TEXTURE0);
gl.glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, RESOLUTION_X, RESOLUTION_Y,
textrender_set_text(_("int main(int argc, char** argv)\n"
"{\n"
" printf(\"Hello World!\");\n"
"}"));
gl_functions.glActiveTexture(GL_TEXTURE0);
gl_functions.glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, RESOLUTION_X, RESOLUTION_Y,
0, GL_RED, GL_UNSIGNED_BYTE, textrender_buffer);
////////////

play_sound();
sound_play();
mainloop(program);

cleanup_sdl();
cleanup();

exit_(0);
clib_exit(0);
}

2 changes: 2 additions & 0 deletions scene_state.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef SCENE_STATE_H
#define SCENE_STATE_H

#include "vector.h"

struct scene_state
{
vector3 position;
Expand Down
15 changes: 8 additions & 7 deletions sdl_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// Not as preprocessor define for consistency with the hacky strings
// for libc- and libgl-names
static const char* sdl_library = "libSDL-1.2.so";
static const char* sdl_functions_library = "libSDL-1.2.so";

// the members of this struct have to have the same order as in the hashes-array!
static struct {
Expand All @@ -19,9 +19,9 @@ static struct {
void SDLCALL (*SDL_GL_SwapBuffers)();
int SDLCALL (*SDL_OpenAudio)(SDL_AudioSpec*, SDL_AudioSpec*);
void SDLCALL (*SDL_PauseAudio)(int);
} sdl;
} sdl_functions;

static const gnu_hash_t sdl_hashes[] = {
static const gnu_hash_t sdl_functions_hashes[] = {
0x70a9a253, // SDL_GL_GetProcAddress
0x7cc5e50f, // SDL_SetVideoMode
0xdcc5fcc6, // SDL_ShowCursor
Expand All @@ -33,13 +33,14 @@ static const gnu_hash_t sdl_hashes[] = {
0x42850c57 // SDL_PauseAudio
};

static stdcall void initialize_sdl_functions()
static stdcall void sdl_functions_initialize()
{
load_library(sdl_library);
linker_load_library(sdl_functions_library);

for (int i = 0; i < sizeof(sdl_hashes) / sizeof(gnu_hash_t); i++)
for (int i = 0; i < sizeof(sdl_functions_hashes) / sizeof(gnu_hash_t); i++)
{
((void**)&sdl)[i] = resolve_symbol(sdl_library, sdl_hashes[i]);
((void**)&sdl_functions)[i] = linker_lookup_symbol(
sdl_functions_library, sdl_functions_hashes[i]);
}
}

Expand Down
Loading

0 comments on commit 9b41f04

Please sign in to comment.