Created
May 3, 2011 15:46
-
-
Save neuro-sys/953580 to your computer and use it in GitHub Desktop.
starfield try
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <windows.h> | |
#include <string.h> | |
#include "SDL.h" | |
#include "SDL_ttf.h" | |
#define WIDTH 1024 | |
#define HEIGHT 600 | |
#define VIEW_DISTANCE HEIGHT | |
#define FAR_Z 2000 | |
#define NEAR_Z 20 | |
#define DENEME | |
#define STAR_NUM 200 | |
RECT rect; | |
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel) | |
{ | |
int bpp = surface->format->BytesPerPixel; | |
/* Here p is the address to the pixel we want to set */ | |
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; | |
switch(bpp) { | |
case 1: | |
*p = pixel; | |
break; | |
case 2: | |
*(Uint16 *)p = pixel; | |
break; | |
case 3: | |
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { | |
p[0] = (pixel >> 16) & 0xff; | |
p[1] = (pixel >> 8) & 0xff; | |
p[2] = pixel & 0xff; | |
} else { | |
p[0] = pixel & 0xff; | |
p[1] = (pixel >> 8) & 0xff; | |
p[2] = (pixel >> 16) & 0xff; | |
} | |
break; | |
case 4: | |
*(Uint32 *)p = pixel; | |
break; | |
} | |
} | |
/* The screen surface */ | |
SDL_Surface *screen = NULL; | |
TTF_Font *font; | |
char fpstext[5] = "...."; | |
typedef struct _STAR { | |
int x, | |
y, | |
z; | |
} STAR; | |
STAR stars[STAR_NUM]; | |
void init_stars() | |
{ | |
int i; | |
for (i = 0; i < STAR_NUM; i++) { | |
stars[i].x = WIDTH / 2 - (rand() % WIDTH); | |
stars[i].y = HEIGHT / 2 - (rand() % HEIGHT); | |
stars[i].z = NEAR_Z + rand() % (FAR_Z - NEAR_Z); | |
} | |
} | |
void draw_stars() | |
{ | |
int i; | |
Uint32 color; | |
for (i = 0; i < STAR_NUM; i++) { | |
int x2d = (WIDTH / 2) + stars[i].x * VIEW_DISTANCE / stars[i].z; | |
int y2d = (HEIGHT / 2) + stars[i].y * VIEW_DISTANCE / stars[i].z; | |
if (x2d <= 0 || x2d >= WIDTH || y2d <= 0 || y2d >= HEIGHT) { | |
// 2d Clipping... | |
continue; | |
} | |
color = SDL_MapRGB (screen->format, 150, 200, 255); | |
putpixel(screen, x2d, y2d, color); | |
} | |
} | |
void move_stars() | |
{ | |
int i; | |
for (i = 0; i < STAR_NUM; i++) { | |
stars[i].z -= 10; | |
if (stars[i].z <= NEAR_Z) { | |
stars[i].z = FAR_Z; | |
} | |
} | |
} | |
static void | |
draw () | |
{ | |
Uint32 color; | |
/* Create a black background */ | |
color = SDL_MapRGB (screen->format, 0, 0, 0); | |
SDL_FillRect (screen, NULL, color); | |
draw_stars(); | |
SDL_Color colort = {100,150,250}; | |
SDL_Surface *text = TTF_RenderUTF8_Solid(font, fpstext, colort); | |
SDL_BlitSurface(text, NULL, screen, NULL); | |
SDL_Flip (screen); | |
SDL_Delay (20); | |
} | |
int | |
main (int argc, char *argv[]) | |
{ | |
char *msg; | |
int done; | |
/* Initialize SDL */ | |
if (SDL_Init (SDL_INIT_VIDEO) < 0) | |
{ | |
asprintf (&msg, "Couldn't initialize SDL: %s\n", SDL_GetError ()); | |
MessageBox (0, msg, "Error", MB_ICONHAND); | |
free (msg); | |
exit (1); | |
} | |
atexit (SDL_Quit); | |
/* Set 640x480 16-bits video mode */ | |
screen = SDL_SetVideoMode (WIDTH, HEIGHT, 8, SDL_SWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN); | |
if (screen == NULL) | |
{ | |
asprintf (&msg, "Couldn't set 640x480x16 video mode: %s\n", | |
SDL_GetError ()); | |
MessageBox (0, msg, "Error", MB_ICONHAND); | |
free (msg); | |
exit (2); | |
} | |
TTF_Init(); | |
font = TTF_OpenFont("C:\\WINDOWS\\Fonts\\cour.ttf", 12); | |
init_stars(); | |
int fps; | |
int timer = SDL_GetTicks() + 1000; | |
done = 0; | |
while (!done) | |
{ | |
SDL_Event event; | |
/* Check for events */ | |
while (SDL_PollEvent (&event)) | |
{ | |
switch (event.type) | |
{ | |
case SDL_KEYDOWN: | |
switch (event.key.keysym.sym) { | |
case SDLK_q: | |
case SDLK_ESCAPE: | |
done = 1; | |
break; | |
} | |
break; | |
case SDL_QUIT: | |
done = 1; | |
break; | |
default: | |
break; | |
} | |
} | |
move_stars(); | |
fps++; | |
if (SDL_GetTicks() >= timer) { | |
itoa(fps, fpstext, 10); | |
fps = 0; | |
timer = SDL_GetTicks() + 1000; | |
} | |
draw(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment