this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Added tutorial-space demo.

+204 -1
+4 -1
demos/CMakeLists.txt
··· 114 114 target_link_libraries (wrap-demo SDL_gpu) 115 115 116 116 add_executable(upload-image-demo upload-image/main.c common/common.c common/demo-font.c) 117 - target_link_libraries (upload-image-demo SDL_gpu) 117 + target_link_libraries (upload-image-demo SDL_gpu) 118 + 119 + add_executable(tutorial-space-demo tutorial-space/main.c common/common.c) 120 + target_link_libraries (tutorial-space-demo SDL_gpu)
demos/data/tutorial-space/ship.png

This is a binary file and will not be displayed.

+200
demos/tutorial-space/main.c
··· 1 + #include "SDL_gpu.h" 2 + #include <math.h> 3 + 4 + #ifndef M_PI 5 + #define M_PI 3.14159265358979323846 6 + #endif 7 + 8 + 9 + typedef struct Ship 10 + { 11 + GPU_Image* image; 12 + 13 + // Motion 14 + float pos_x, pos_y; 15 + float vel_x, vel_y; 16 + float angle; 17 + 18 + // Properties of the ship 19 + float thrust; 20 + float drag_coefficient; 21 + } Ship; 22 + 23 + Ship* create_ship(const char* image_file) 24 + { 25 + Ship* ship = malloc(sizeof(Ship)); 26 + if(ship == NULL) 27 + return NULL; 28 + 29 + ship->image = GPU_LoadImage(image_file); 30 + ship->pos_x = 0.0f; 31 + ship->pos_y = 0.0f; 32 + ship->vel_x = 0.0f; 33 + ship->vel_y = 0.0f; 34 + ship->angle = 0.0f; 35 + ship->thrust = 500.0f; 36 + ship->drag_coefficient = 0.00005f; 37 + 38 + return ship; 39 + } 40 + 41 + void free_ship(Ship* ship) 42 + { 43 + if(ship == NULL) 44 + return; 45 + 46 + GPU_FreeImage(ship->image); 47 + free(ship); 48 + } 49 + 50 + void apply_thrust(Ship* ship, float dt) 51 + { 52 + ship->vel_x += ship->thrust * cosf(ship->angle) * dt; 53 + ship->vel_y += ship->thrust * sinf(ship->angle) * dt; 54 + } 55 + 56 + void apply_drag(Ship* ship, float dt) 57 + { 58 + float vel_angle = atan2f(ship->vel_y, ship->vel_x); 59 + float vel = sqrtf(ship->vel_x*ship->vel_x + ship->vel_y*ship->vel_y); 60 + vel -= ship->drag_coefficient * vel * vel; 61 + if(vel < 0) 62 + vel = 0; 63 + 64 + ship->vel_x = vel * cosf(vel_angle); 65 + ship->vel_y = vel * sinf(vel_angle); 66 + } 67 + 68 + void update_ship(Ship* ship, GPU_Rect play_area, float dt) 69 + { 70 + ship->pos_x += ship->vel_x * dt; 71 + ship->pos_y += ship->vel_y * dt; 72 + 73 + if(ship->pos_x < play_area.x) 74 + { 75 + ship->pos_x = play_area.x; 76 + ship->vel_x = -ship->vel_x; 77 + } 78 + else if(ship->pos_x >= play_area.x + play_area.w) 79 + { 80 + ship->pos_x = play_area.x + play_area.w; 81 + ship->vel_x = -ship->vel_x; 82 + } 83 + 84 + if(ship->pos_y < play_area.y) 85 + { 86 + ship->pos_y = play_area.y; 87 + ship->vel_y = -ship->vel_y; 88 + } 89 + else if(ship->pos_y >= play_area.y + play_area.h) 90 + { 91 + ship->pos_y = play_area.y + play_area.h; 92 + ship->vel_y = -ship->vel_y; 93 + } 94 + } 95 + 96 + void draw_ship(Ship* ship, GPU_Target* screen) 97 + { 98 + GPU_BlitRotate(ship->image, NULL, screen, ship->pos_x, ship->pos_y, ship->angle * 180 / M_PI); 99 + } 100 + 101 + 102 + 103 + int main(int argc, char* argv[]) 104 + { 105 + GPU_SetDebugLevel(GPU_DEBUG_LEVEL_MAX); 106 + 107 + GPU_Target* screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS); 108 + if(screen == NULL) 109 + return -1; 110 + 111 + 112 + // Game variables 113 + 114 + Ship* player_ship = create_ship("data/tutorial-space/ship.png"); 115 + 116 + if(player_ship == NULL || player_ship->image == NULL) 117 + { 118 + free_ship(player_ship); 119 + return -2; 120 + } 121 + 122 + GPU_Rect play_area = {0, 0, screen->w, screen->h}; 123 + 124 + player_ship->pos_x = play_area.x + play_area.w/2; 125 + player_ship->pos_y = play_area.y + play_area.h/2; 126 + 127 + 128 + // Interaction variables 129 + 130 + SDL_Event event; 131 + Uint32 mouse_state; 132 + int mouse_x, mouse_y; 133 + 134 + 135 + // Loop variables 136 + 137 + Uint32 start_time = SDL_GetTicks(); 138 + Uint32 end_time; 139 + float dt = 0.0f; 140 + 141 + Uint8 done = 0; 142 + 143 + 144 + // Game loop 145 + 146 + while(!done) 147 + { 148 + // Check events 149 + 150 + while(SDL_PollEvent(&event)) 151 + { 152 + if(event.type == SDL_QUIT) 153 + done = 1; 154 + else if(event.type == SDL_KEYDOWN) 155 + { 156 + if(event.key.keysym.sym == SDLK_ESCAPE) 157 + done = 1; 158 + } 159 + } 160 + 161 + // Update 162 + 163 + mouse_state = SDL_GetMouseState(&mouse_x, &mouse_y); 164 + 165 + apply_drag(player_ship, dt); 166 + 167 + player_ship->angle = atan2f(mouse_y - player_ship->pos_y, mouse_x - player_ship->pos_x); 168 + if(mouse_state & SDL_BUTTON_LMASK) 169 + apply_thrust(player_ship, dt); 170 + 171 + update_ship(player_ship, play_area, dt); 172 + 173 + 174 + // Draw 175 + 176 + GPU_Clear(screen); 177 + 178 + draw_ship(player_ship, screen); 179 + 180 + GPU_Flip(screen); 181 + 182 + 183 + // Timing 184 + 185 + SDL_Delay(10); 186 + end_time = SDL_GetTicks(); 187 + dt = (end_time - start_time)/1000.0f; 188 + start_time = end_time; 189 + } 190 + 191 + 192 + // Clean up 193 + 194 + free_ship(player_ship); 195 + GPU_Quit(); 196 + 197 + return 0; 198 + } 199 + 200 +