Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #define SCREEN_WIDTH 320
- #define SCREEN_HEIGHT 255
- #define STARS_VOL 15000
- #define STARS_DEPTH 127
- #define STARS_NUM 1024
- typedef struct
- {
- signed int x, y, z;
- } STAR;
- STAR starBuffer[STARS_NUM];
- float sineGradian, sineGradian2, sinePage, sinePage2;
- void starField_init(void)
- {
- int i;
- sineGradian = 0.0f;
- sineGradian2 = 0.0f;
- sinePage = 0.0f;
- sinePage2 = 0.0f;
- srand((unsigned int)time(NULL));
- for (i = 0; i < STARS_NUM; i++)
- {
- signed int randVal1 = STARS_VOL - (rand() % (STARS_VOL * 2));
- signed int randVal2 = STARS_VOL - (rand() % (STARS_VOL * 2));
- starBuffer[i].x = randVal1;
- starBuffer[i].y = randVal2;
- starBuffer[i].z = (rand() % (STARS_DEPTH - 1)) + 1;
- }
- }
- void starField_draw(unsigned int *buffer)
- {
- int i;
- sinePage += 0.5f;
- if (sinePage >= 360.0f)
- {
- sinePage -= 360.0f;
- }
- sineGradian = sinePage;
- for (i = 0; i < STARS_NUM; i++)
- {
- float f = sineGradian * (3.1415927f / 180.0f);
- /* 2D point rotation, exclude depth */
- int newx = (int)((starBuffer[i].x * cosf(f)) - (starBuffer[i].y * sinf(f)));
- int newy = (int)((starBuffer[i].x * sinf(f)) + (starBuffer[i].y * cosf(f)));
- newx = (newx / starBuffer[i].z) + (SCREEN_WIDTH / 2);
- newy = (newy / starBuffer[i].z) + (SCREEN_HEIGHT / 2);
- /* put pixels if not outside of screen */
- if (((newx >= 0) && (newx < SCREEN_WIDTH))
- && ((newy >= 0) && (newy < SCREEN_HEIGHT)))
- {
- buffer[(newy * SCREEN_WIDTH) + newx] = 0x00010101 * (255 - (starBuffer[i].z * 2));
- }
- /* decrease depth index */
- starBuffer[i].z--;
- /* reset depth index if close to screen */
- if (starBuffer[i].z <= 1)
- {
- starBuffer[i].z = STARS_DEPTH;
- }
- }
- }
- void initAboutScreenEffects(void)
- {
- starField_init();
- }
- void drawAboutScreen(unsigned int *buffer)
- {
- starField_draw(buffer);
- }
Add Comment
Please, Sign In to add comment