Guest User

Untitled

a guest
Jan 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5.  
  6. #define SCREEN_WIDTH 320
  7. #define SCREEN_HEIGHT 255
  8.  
  9. #define STARS_VOL 15000
  10. #define STARS_DEPTH 127
  11. #define STARS_NUM 1024
  12.  
  13. typedef struct
  14. {
  15.     signed int x, y, z;
  16. } STAR;
  17.  
  18. STAR starBuffer[STARS_NUM];
  19.  
  20. float sineGradian, sineGradian2, sinePage, sinePage2;
  21.  
  22. void starField_init(void)
  23. {
  24.     int i;
  25.  
  26.     sineGradian = 0.0f;
  27.     sineGradian2 = 0.0f;
  28.     sinePage = 0.0f;
  29.     sinePage2 = 0.0f;
  30.  
  31.     srand((unsigned int)time(NULL));
  32.  
  33.     for (i = 0; i < STARS_NUM; i++)
  34.     {
  35.         signed int randVal1 = STARS_VOL - (rand() % (STARS_VOL * 2));
  36.         signed int randVal2 = STARS_VOL - (rand() % (STARS_VOL * 2));
  37.  
  38.         starBuffer[i].x = randVal1;
  39.         starBuffer[i].y = randVal2;
  40.         starBuffer[i].z = (rand() % (STARS_DEPTH - 1)) + 1;
  41.     }
  42. }
  43.  
  44. void starField_draw(unsigned int *buffer)
  45. {
  46.     int i;
  47.  
  48.     sinePage += 0.5f;
  49.     if (sinePage >= 360.0f)
  50.     {
  51.         sinePage -= 360.0f;
  52.     }
  53.     sineGradian = sinePage;
  54.  
  55.     for (i = 0; i < STARS_NUM; i++)
  56.     {
  57.         float f = sineGradian  * (3.1415927f / 180.0f);
  58.  
  59.         /* 2D point rotation, exclude depth */
  60.         int newx = (int)((starBuffer[i].x * cosf(f)) - (starBuffer[i].y * sinf(f)));
  61.         int newy = (int)((starBuffer[i].x * sinf(f)) + (starBuffer[i].y * cosf(f)));
  62.        
  63.         newx = (newx / starBuffer[i].z) + (SCREEN_WIDTH / 2);
  64.         newy = (newy / starBuffer[i].z) + (SCREEN_HEIGHT / 2);
  65.  
  66.         /* put pixels if not outside of screen */
  67.         if (((newx >= 0) && (newx < SCREEN_WIDTH))
  68.             && ((newy >= 0) && (newy < SCREEN_HEIGHT)))
  69.         {
  70.             buffer[(newy * SCREEN_WIDTH) + newx] = 0x00010101 * (255 - (starBuffer[i].z * 2));
  71.         }
  72.  
  73.         /* decrease depth index */
  74.         starBuffer[i].z--;
  75.  
  76.         /* reset depth index if close to screen */
  77.         if (starBuffer[i].z <= 1)
  78.         {
  79.             starBuffer[i].z = STARS_DEPTH;
  80.         }
  81.     }
  82. }
  83.  
  84. void initAboutScreenEffects(void)
  85. {
  86.     starField_init();
  87. }
  88.  
  89. void drawAboutScreen(unsigned int *buffer)
  90. {
  91.     starField_draw(buffer);
  92. }
Add Comment
Please, Sign In to add comment