From e5988e596c20bccb97032dcd70072df010e403b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sun, 23 Jun 2019 17:32:53 +0200 Subject: [PATCH] Tidy examples --- programs/city.c | 59 ++++++++++++++++++++++-------------------------- programs/level.c | 43 +++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/programs/city.c b/programs/city.c index 4b8166b..efae9bf 100644 --- a/programs/city.c +++ b/programs/city.c @@ -1,6 +1,8 @@ /* + Example program for small3dlib -- a GTA-like game demo. + author: Miloslav Ciz - license: CC0 + license: CC0 1.0 */ #include @@ -96,18 +98,18 @@ static inline void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t bl pixels[y * S3L_RESOLUTION_X + x] = r | g | b; } -void sampleTexture(uint8_t *texture, int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b) +void sampleTexture(int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b) { - u = S3L_clamp(u,0,TEXTURE_W - 1); - v = S3L_clamp(v,0,TEXTURE_H - 1); + u = S3L_clamp(u,0,CITY_TEXTURE_WIDTH - 1); + v = S3L_clamp(v,0,CITY_TEXTURE_HEIGHT - 1); - int32_t index = (v * TEXTURE_W + u) * 3; + int32_t index = (v * CITY_TEXTURE_WIDTH + u) * 3; - *r = texture[index]; + *r = cityTexture[index]; index++; - *g = texture[index]; + *g = cityTexture[index]; index++; - *b = texture[index]; + *b = cityTexture[index]; } uint32_t previousTriangle = -1; @@ -136,14 +138,14 @@ void drawPixel(S3L_PixelInfo *p) previousTriangle = p->triangleID; } - uint8_t r,g,b; + uint8_t r, g, b; S3L_Unit uv[2]; uv[0] = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric); uv[1] = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric); - sampleTexture(cityTexture,uv[0] / 2,uv[1] / 2,&r,&g,&b); + sampleTexture(uv[0] / 2,uv[1] / 2,&r,&g,&b); setPixel(p->x,p->y,r,g,b); } @@ -151,9 +153,7 @@ void drawPixel(S3L_PixelInfo *p) void draw() { S3L_newFrame(); - clearScreen(); - S3L_drawScene(scene); } @@ -167,29 +167,28 @@ static inline uint32_t collision(S3L_Vec4 worldPosition) return collisionMap[index]; } -void handleCollision(S3L_Vec4 *pos, S3L_Vec4 previousPos) +static inline void handleCollision(S3L_Vec4 *pos, S3L_Vec4 previousPos) { - S3L_Vec4 newPos = *pos; - newPos.x = previousPos.x; - - if (collision(newPos)) - { - newPos = *pos; - newPos.z = previousPos.z; + S3L_Vec4 newPos = *pos; + newPos.x = previousPos.x; + + if (collision(newPos)) + { + newPos = *pos; + newPos.z = previousPos.z; - if (collision(newPos)) - newPos = previousPos; - } - - *pos = newPos; + if (collision(newPos)) + newPos = previousPos; + } + *pos = newPos; } int16_t fps = 0; int main() { - SDL_Window *window = SDL_CreateWindow("model viewer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, S3L_RESOLUTION_X, S3L_RESOLUTION_Y, SDL_WINDOW_SHOWN); + SDL_Window *window = SDL_CreateWindow("city demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, S3L_RESOLUTION_X, S3L_RESOLUTION_Y, SDL_WINDOW_SHOWN); SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,0); SDL_Texture *textureSDL = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, S3L_RESOLUTION_X, S3L_RESOLUTION_Y); SDL_Surface *screenSurface = SDL_GetWindowSurface(window); @@ -198,16 +197,12 @@ int main() cityModelInit(); carModelInit(); - carModel.transform.translation.x = 7 * (S3L_FRACTIONS_PER_UNIT / 2); - carModel.transform.translation.z = -7 * (S3L_FRACTIONS_PER_UNIT / 2); - carModel.transform.translation.y = (S3L_FRACTIONS_PER_UNIT / 32); - models[0] = cityModel; models[1] = carModel; S3L_initScene(models,2,&scene); - scene.camera.transform.translation.z = -S3L_FRACTIONS_PER_UNIT * 8; + S3L_setTransform3D(1909,16,-3317,0,-510,0,512,512,512,&(models[1].transform)); int running = 1; @@ -224,7 +219,7 @@ int main() int16_t velocity = 0; - while (running) + while (running) // main loop { clock_t frameStartT = clock(); diff --git a/programs/level.c b/programs/level.c index 925ae48..60e8613 100644 --- a/programs/level.c +++ b/programs/level.c @@ -1,6 +1,8 @@ /* + Example program for small3dlib, showing a Quake-like level. + author: Miloslav Ciz - license: CC0 + license: CC0 1.0 */ #include @@ -118,23 +120,26 @@ void drawPixel(S3L_PixelInfo *p) { if (p->triangleID != previousTriangle) { - if (p->modelIndex == 0) + switch (p->modelIndex) { - uvs = levelWallsUVs; - uvIndices = levelWallsUVIndices; - texture = level1Texture; - } - else if (p->modelIndex == 1) - { - uvs = levelFloorUVs; - uvIndices = levelFloorUVIndices; - texture = level2Texture; - } - else - { - uvs = levelCeilingUVs; - uvIndices = levelCeilingUVIndices; - texture = level3Texture; + case 0: + uvs = levelWallsUVs; + uvIndices = levelWallsUVIndices; + texture = level1Texture; + break; + + case 1: + uvs = levelFloorUVs; + uvIndices = levelFloorUVIndices; + texture = level2Texture; + break; + + case 2: + default: + uvs = levelCeilingUVs; + uvIndices = levelCeilingUVIndices; + texture = level3Texture; + break; } S3L_getIndexedTriangleValues(p->triangleIndex,uvIndices,uvs,2,&uv0,&uv1,&uv2); @@ -205,7 +210,7 @@ void draw() int main() { - SDL_Window *window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, S3L_RESOLUTION_X, S3L_RESOLUTION_Y, SDL_WINDOW_SHOWN); + SDL_Window *window = SDL_CreateWindow("level demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, S3L_RESOLUTION_X, S3L_RESOLUTION_Y, SDL_WINDOW_SHOWN); SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,0); SDL_Texture *texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, S3L_RESOLUTION_X, S3L_RESOLUTION_Y); SDL_Surface *screenSurface = SDL_GetWindowSurface(window); @@ -248,7 +253,7 @@ int main() int running = 1; - while (running) + while (running) // main loop { draw(); SDL_UpdateTexture(texture,NULL,pixels,S3L_RESOLUTION_X * sizeof(uint32_t));