int main (int argc, char* args[]) { //SDL Window setup if (init(SCREEN_WIDTH, SCREEN_HEIGHT) == 1) { return 0; } int sleep = 0; int quit = 0; Uint32 next_game_tick = SDL_GetTicks(); Uint32 current_tick = next_game_tick; clear_pixels(&cm, 0); clear_pixels(&pb, 0); clear_pixels(&fire, 0); clear_pixels(&cool, 0); Uint8 r = 255, g = 255, b = 255; int i; //create fire colour pallet and store colour values in a pixel buffer for (i = 0; i < cm.width; i++) { Uint32 colour = r << 24 | g << 16 | b << 8; int p1 = 85; int p2 = 170; if (i <= p1) { float inverse = p1 - i; float interp = inverse / p1; b = interp * 255; colour = r << 24 | g << 16 | b << 8; } else if (i > p1 && i <= p2) { float inverse = p2 - i; float interp = inverse / p1; g = interp * 255; colour = r << 24 | g << 16 | b << 8; } else { float inverse = cm.width - i; float interp = inverse / p1; r = interp * 255; colour = r << 24 | g << 16 | b << 8; } draw_pixel(&cm, i, 0, colour); } SDL_UpdateTexture(colour_map, NULL, cm.pixels, cm.width * sizeof (Uint32)); for (i = 0; i < 10000; i++) { int x = rand() % cool.width; int y = rand() % cool.height; draw_pixel(&cool, x, y, 255); } smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); smooth_buffer(&cool); int offset = 0; //render loop while(quit == 0) { //check for new events every frame SDL_PumpEvents(); const Uint8 *state = SDL_GetKeyboardState(NULL); if (state[SDL_SCANCODE_ESCAPE]) { quit = 1; } //init the fire array for (i = 0; i < 550; i++) { //random y and x values int x = rand() % fire.width; int y = rand() % 2 + fire.height - 2; draw_pixel(&fire, x, y, 255); } draw_circle(&fire, 100, 100, 5, 255); //heat spread smoothing smooth_buffer(&fire); int x,y; //cooling for (x = 0; x < fire.width; x++) { for (y = 0; y < fire.height; y++) { Uint32 val = get_pixel(&fire, x, y); Uint32 cool_val = get_pixel(&cool, x, (y + offset) % cool.height); if (cool_val == 0) { cool_val = rand() % 3; } int res = val - cool_val; if (res > 0) { draw_pixel(&fire, x, y - 1, res); } } } offset += 3; //draw to the screen buffer for (x = 0; x < pb.width; x++) { for (y = 0; y < pb.height; y++) { Uint32 p = get_pixel(&fire, x, y); Uint32 colour = get_pixel(&cm, 255 - p, 0); draw_pixel(&pb, x, y, colour); } } SDL_UpdateTexture(screen, NULL, pb.pixels, pb.width * sizeof (Uint32)); //draw to the screen SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); SDL_RenderClear(renderer); SDL_RenderCopy(renderer, screen, NULL, NULL); SDL_RenderPresent(renderer); //show final frame on the screen //time it takes to render 1 frame in milliseconds next_game_tick += 1000 / 60; current_tick = SDL_GetTicks(); sleep = next_game_tick - current_tick; if( sleep >= 0 ) { SDL_Delay(sleep); } } free(pb.pixels); free(cm.pixels); free(fire.pixels); //Destroy window SDL_DestroyWindow(window); //Quit SDL subsystems SDL_Quit(); return 0; }
int main (int argc, char* args[]) { //SDL Window setup if (init(SCREEN_WIDTH, SCREEN_HEIGHT) == 1) { return 0; } //initilise vector_balls init_vector_balls(balls); //create circle image in pixel buffer and copy to a texture clear_pixels(&vb, 0x00000000); draw_circle(&vb, vb.width / 2, vb.height / 2, 8, 0xffffffff); SDL_UpdateTexture(vector_ball, NULL, vb.pixels, vb.width * sizeof (uint32_t)); int sleep = 0; int quit = 0; Uint32 next_game_tick = SDL_GetTicks(); Uint32 current_tick = next_game_tick; float d = 1; //render loop while(quit == 0) { //check for new events every frame SDL_PumpEvents(); const Uint8 *state = SDL_GetKeyboardState(NULL); if (state[SDL_SCANCODE_ESCAPE]) { quit = 1; } //draw to the screen SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); draw_vector_balls(renderer, vector_ball, balls, SIZE, SCREEN_WIDTH, SCREEN_HEIGHT); //SDL_RenderCopy(renderer, vector_ball, NULL, NULL); //draw screen pixel_buffer SDL_RenderPresent(renderer); //show final frame on the screen update_balls(balls, SIZE, d); //d+=.1; //time it takes to render 1 frame in milliseconds next_game_tick += 1000 / 60; current_tick = SDL_GetTicks(); sleep = next_game_tick - current_tick; if( sleep >= 0 ) { SDL_Delay(sleep); } } //free the vector_ball buffer free(vb.pixels); //Destroy window SDL_DestroyWindow(window); //Quit SDL subsystems SDL_Quit(); return 0; }
int main (int argc, char* args[]) { //SDL Window setup if (init(SCREEN_WIDTH, SCREEN_HEIGHT) == 1) { return 0; } init_stars(snow, SCREEN_WIDTH, SCREEN_HEIGHT); //create circle image in pixel buffer and copy to a texture draw_circle(&vb, vb.width / 2, vb.height / 2, 8, 0xffffffff); SDL_UpdateTexture(vector_ball, NULL, vb.pixels, vb.width * sizeof (uint32_t)); int sleep = 0; int quit = 0; struct vector3d vel = {0, 0, -.005}; //snow movment vector Uint32 next_game_tick = SDL_GetTicks(); Uint32 current_tick = next_game_tick; //render loop while(quit == 0) { //check for new events every frame SDL_PumpEvents(); const Uint8 *state = SDL_GetKeyboardState(NULL); if (state[SDL_SCANCODE_ESCAPE]) { quit = 1; } clear_pixels(&pb, 0x00000000); SDL_UpdateTexture(screen, NULL, pb.pixels, pb.width * sizeof (uint32_t)); //draw to the screen SDL_RenderClear(renderer); SDL_RenderCopy(renderer, screen, NULL, NULL); //draw screen pixel_buffer draw_stars(renderer, vector_ball, snow); //draw all snow flakes with vector ball texture SDL_RenderPresent(renderer); //show fintal frame on the screen update_stars(snow, SCREEN_WIDTH, SCREEN_HEIGHT, &vel); //time it takes to render 1 frame in milliseconds next_game_tick += 1000 / 60; current_tick = SDL_GetTicks(); sleep = next_game_tick - current_tick; if( sleep >= 0 ) { SDL_Delay(sleep); } } //free the screen buffer free(pb.pixels); //free the vector_ball buffer free(vb.pixels); //free the source image SDL_FreeSurface(source); //Destroy window SDL_DestroyWindow(window); //Quit SDL subsystems SDL_Quit(); return 0; }
int main (int argc, char* args[]) { //SDL Window setup if (init(SCREEN_WIDTH, SCREEN_HEIGHT) == 1) { return 0; } int i = 0; int j = 0; int offset = 0; struct vector2d translation = {-SCREEN_WIDTH / 2, -SCREEN_HEIGHT / 2}; //set up icons used to represent player lives for (i = 0; i < LIVES; i++) { init_player(&lives[i]); lives[i].lives = 1; //shrink lives for (j = 0; j < P_VERTS; j++) { divide_vector(&lives[i].obj_vert[j], 2); } //convert screen space vector into world space struct vector2d top_left = {20 + offset, 20}; add_vector(&top_left, &translation); lives[i].location = top_left; update_player(&lives[i]); offset += 20; } //set up player and asteroids in world space init_player(&p); init_asteroids(asteroids, ASTEROIDS); int sleep = 0; int quit = 0; SDL_Event event; Uint32 next_game_tick = SDL_GetTicks(); //render loop while(quit == 0) { //check for new events every frame SDL_PumpEvents(); const Uint8 *state = SDL_GetKeyboardState(NULL); if (state[SDL_SCANCODE_ESCAPE]) { quit = 1; } if (state[SDL_SCANCODE_UP]) { struct vector2d thrust = get_direction(&p); multiply_vector(&thrust, .06); apply_force(&p.velocity, thrust); } if (state[SDL_SCANCODE_LEFT]) { rotate_player(&p, -4); } if (state[SDL_SCANCODE_RIGHT]) { rotate_player(&p, 4); } while (SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: switch( event.key.keysym.sym ) { case SDLK_SPACE: if (p.lives > 0) { shoot_bullet(&p); } break; } } } //draw to the pixel buffer clear_pixels(pixels, 0x00000000); draw_player(pixels, &p); draw_player(pixels, &lives[0]); draw_player(pixels, &lives[1]); draw_player(pixels, &lives[2]); draw_asteroids(pixels, asteroids, ASTEROIDS); update_player(&p); bounds_player(&p); bounds_asteroids(asteroids, ASTEROIDS); int res = collision_asteroids(asteroids, ASTEROIDS, &p.location, p.hit_radius); if (res != -1) { p.lives--; p.location.x = 0; p.location.y = 0; p.velocity.x = 0; p.velocity.y = 0; int i = LIVES - 1; for ( i = LIVES; i >= 0; i--) { if(lives[i].lives > 0) { lives[i].lives = 0; break; } } } int i = 0; struct vector2d translation = {-SCREEN_WIDTH / 2, -SCREEN_HEIGHT / 2}; for (i = 0; i < BULLETS; i++) { //only check for collision for bullets that are shown on screen if (p.bullets[i].alive == TRUE) { //convert bullet screen space location to world space to compare //with asteroids world space to detect a collision struct vector2d world = add_vector_new(&p.bullets[i].location, &translation); int index = collision_asteroids(asteroids, ASTEROIDS, &world, 1); //collision occured if (index != -1) { asteroids[index].alive = 0; p.bullets[i].alive = FALSE; if (asteroids[index].size != SMALL) { spawn_asteroids(asteroids, ASTEROIDS, asteroids[index].size, asteroids[index].location); } } } } update_asteroids(asteroids, ASTEROIDS); //draw buffer to the texture representing the screen SDL_UpdateTexture(screen, NULL, pixels, SCREEN_WIDTH * sizeof (Uint32)); //draw to the screen SDL_RenderClear(renderer); SDL_RenderCopy(renderer, screen, NULL, NULL); SDL_RenderPresent(renderer); //time it takes to render 1 frame in milliseconds next_game_tick += 1000 / 60; sleep = next_game_tick - SDL_GetTicks(); if( sleep >= 0 ) { SDL_Delay(sleep); } } //free the screen buffer free(pixels); //Destroy window SDL_DestroyWindow(window); //Quit SDL subsystems SDL_Quit(); return 0; }