// Game loop while (running) current_time = SDL_GetTicks(); delta_time = (current_time - last_time) / 1000.0f; last_time = current_time; // Input handling handle_input(&event, player, &running); // Update update_position(player); update_animation(player); // Render SDL_SetRenderDrawColor(renderer, 30, 30, 50, 255); SDL_RenderClear(renderer); // Draw grid for visual reference SDL_SetRenderDrawColor(renderer, 60, 60, 80, 255); for (int x = 0; x < SCREEN_WIDTH; x += 50) SDL_RenderLine(renderer, x, 0, x, SCREEN_HEIGHT); SDL_RenderLine(renderer, 0, x, SCREEN_WIDTH, x); // Draw sprite render_sprite(renderer, player); // Display info text (using debug overlay) char info[256]; snprintf(info, sizeof(info), "Frame: %d/%d
// Load texture (assumes sprite sheet is 4 frames horizontally) sprite->texture = SDL_LoadTexture(renderer, filename); if (!sprite->texture) printf("Failed to load texture: %s\n", SDL_GetError()); free(sprite); return NULL; sdl3 tutorial
// Update animation frame void update_animation(AnimatedSprite* sprite) if (sprite->moving) sprite->frame_counter++; if (sprite->frame_counter >= sprite->frame_delay) sprite->frame_counter = 0; sprite->current_frame = (sprite->current_frame + 1) % FRAME_COUNT; // Game loop while (running) current_time = SDL_GetTicks();
// Update position based on velocity void update_position(AnimatedSprite* sprite) sprite->x += sprite->velocity_x; sprite->y += sprite->velocity_y; last_time = current_time
else // Reset to first frame when idle sprite->current_frame = 0; sprite->frame_counter = 0;
// Main game loop int main(int argc, char* argv[]) // Initialize SDL3 if (!SDL_Init(SDL_INIT_VIDEO)) printf("SDL_Init failed: %s\n", SDL_GetError()); return 1;
SDL_Event event; bool running = true; Uint64 last_time = SDL_GetTicks(); Uint64 current_time; float delta_time;