From 853f9385213300b7d966a90d4da960dae5fb9b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Wed, 12 Sep 2018 17:37:15 +0200 Subject: [PATCH] Add pixel counter --- raycastlib.h | 8 ++++++-- testSDL.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/raycastlib.h b/raycastlib.h index ffa2dbf..30a2294 100644 --- a/raycastlib.h +++ b/raycastlib.h @@ -242,6 +242,8 @@ void castRaysMultiHit(Camera cam, ArrayFunction arrayFunc, /** Using provided functions, renders a complete complex camera view. + This function should render each screen pixel exactly once. + @param cam camera whose view to render @param floorHeightFunc function that returns floor height (in Units) @param ceilingHeightFunc same as floorHeightFunc but for ceiling, can also be @@ -261,6 +263,8 @@ void render(Camera cam, ArrayFunction floorHeightFunc, with simple 1-intersection raycasting. The render(...) function can give more accurate results than this function, so it's to be considered even for simple scenes. + + This function should render each screen pixel exactly once. */ void renderSimple(Camera cam, ArrayFunction floorHeightFunc, ArrayFunction typeFunc, PixelFunction pixelFunc, RayConstraints constraints); @@ -888,8 +892,8 @@ void _columnFunction(HitResult *hits, uint16_t hitCount, uint16_t x, Ray ray) _pixelFunction(&p); } -if (z1ScreenCeil > y2) - y2 = z1ScreenCeil; + if (z1ScreenCeil > y2) + y2 = z1ScreenCeil; } // draw floor wall diff --git a/testSDL.c b/testSDL.c index d5509f5..8f1960c 100644 --- a/testSDL.c +++ b/testSDL.c @@ -21,8 +21,8 @@ #define LEVEL_X_RES 29 #define LEVEL_Y_RES 21 -#define SCREEN_WIDTH 640 -#define SCREEN_HEIGHT 480 +#define SCREEN_WIDTH 320 +#define SCREEN_HEIGHT 240 #define MIDDLE_ROW (SCREEN_HEIGHT / 2) #define TRANSPARENT_COLOR 0b00000111 @@ -48,6 +48,7 @@ Unit zBuffer[SCREEN_WIDTH]; ///< 1D z-buffer for visibility determination. Camera camera; uint32_t pixels[SCREEN_WIDTH * SCREEN_HEIGHT]; +uint32_t pixelCounter[SCREEN_WIDTH * SCREEN_HEIGHT]; typedef struct { @@ -697,6 +698,28 @@ const unsigned char sprite3[] = const unsigned char *textures[] = {texture1, texture2, texture3, texture4}; +void clearPixelCounter() +{ + memset(pixelCounter,0,SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(uint32_t)); +} + +void writePixelCounter() +{ + printf("\npixel counter:\n"); + + for (int y = 0; y < SCREEN_HEIGHT; ++y) + { + for (int x = 0; x < SCREEN_WIDTH; ++x) + { + uint32_t count = pixelCounter[y * SCREEN_WIDTH + x]; + + if (count != 1) // each pixel should have been rendered only once + printf("error: pixel %d %d rendered %d times!\n",x,y,count); + } + + } +} + uint8_t rgbToIndex(uint8_t r, uint8_t g, uint8_t b) { return (r & 0b00000111) | ((g & 0b00000111) << 3) | ((b & 0b00000011) << 6); @@ -860,7 +883,10 @@ void pixelFunc(PixelInfo *pixel) b = b > 0 ? b : 0; b = b << 8; - pixels[pixel->position.y * SCREEN_WIDTH + pixel->position.x] = r | g | b; + int32_t index = pixel->position.y * SCREEN_WIDTH + pixel->position.x; + + pixels[index] = r | g | b; + pixelCounter[index]++; } void draw() @@ -945,10 +971,23 @@ int main() SDL_Event event; int running = 1; + int counter = 0; + while (running) { + if (counter <= 0) + clearPixelCounter(); + draw(); + if (counter <= 0) + { + writePixelCounter(); + counter = 256; + } + else + counter--; + SDL_UpdateTexture(texture,NULL,pixels,SCREEN_WIDTH * sizeof(uint32_t)); while (SDL_PollEvent(&event))