DOlphin progress
This commit is contained in:
27
src2/CMakeLists.txt
Normal file
27
src2/CMakeLists.txt
Normal file
@@ -0,0 +1,27 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Libs
|
||||
|
||||
# Includes
|
||||
target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
# Sources
|
||||
|
||||
# Main Binary Source
|
||||
target_sources(${DUSK_BINARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
main.cpp
|
||||
sdl_starter.cpp
|
||||
sdl_assets_loader.cpp
|
||||
)
|
||||
|
||||
# Defs
|
||||
|
||||
|
||||
# Subdirs
|
||||
159
src2/main.cpp
Normal file
159
src2/main.cpp
Normal file
@@ -0,0 +1,159 @@
|
||||
#include <gccore.h>
|
||||
#include "sdl_starter.h"
|
||||
#include "sdl_assets_loader.h"
|
||||
|
||||
SDL_Window *window = nullptr;
|
||||
SDL_Renderer *renderer = nullptr;
|
||||
|
||||
const int PLAYER_SPEED = 600;
|
||||
|
||||
SDL_Rect player = {SCREEN_WIDTH / 2 - 64, SCREEN_HEIGHT / 2 - 64, 64, 64};
|
||||
|
||||
SDL_Rect ball = {SCREEN_WIDTH / 2 + 50, SCREEN_HEIGHT / 2, 32, 32};
|
||||
|
||||
int ballVelocityX = 400;
|
||||
int ballVelocityY = 400;
|
||||
|
||||
int colorIndex;
|
||||
|
||||
SDL_Color colors[] = {
|
||||
{128, 128, 128, 0}, // gray
|
||||
{255, 255, 255, 0}, // white
|
||||
{255, 0, 0, 0}, // red
|
||||
{0, 255, 0, 0}, // green
|
||||
{0, 0, 255, 0}, // blue
|
||||
{255, 255, 0, 0}, // brown
|
||||
{0, 255, 255, 0}, // cyan
|
||||
{255, 0, 255, 0}, // purple
|
||||
};
|
||||
|
||||
void quitGame()
|
||||
{
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void handleEvents()
|
||||
{
|
||||
SDL_Event event;
|
||||
|
||||
while (SDL_PollEvent(&event))
|
||||
{
|
||||
if (event.type == SDL_QUIT)
|
||||
{
|
||||
quitGame();
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int rand_range(int min, int max)
|
||||
{
|
||||
return min + rand() / (RAND_MAX / (max - min + 1) + 1);
|
||||
}
|
||||
|
||||
void update(float deltaTime)
|
||||
{
|
||||
// PAD_ButtonsDown tells us which buttons were pressed in this loop
|
||||
// this is a "one shot" state which will not fire again until the button has been released
|
||||
const u32 padDown = PAD_ButtonsDown(0);
|
||||
|
||||
// PAD_ButtonsHeld tells us which buttons are keep pressing in this loop
|
||||
const u32 padHeld = PAD_ButtonsHeld(0);
|
||||
|
||||
// We return to the launcher application via exit
|
||||
if (padDown & PAD_BUTTON_START)
|
||||
exit(0);
|
||||
|
||||
if (padHeld & PAD_BUTTON_LEFT && player.x > 0)
|
||||
{
|
||||
player.x -= PLAYER_SPEED * deltaTime;
|
||||
}
|
||||
|
||||
else if (padHeld & PAD_BUTTON_RIGHT && player.x < SCREEN_WIDTH - player.w)
|
||||
{
|
||||
player.x += PLAYER_SPEED * deltaTime;
|
||||
}
|
||||
|
||||
else if (padHeld & PAD_BUTTON_UP && player.y > 0)
|
||||
{
|
||||
player.y -= PLAYER_SPEED * deltaTime;
|
||||
}
|
||||
|
||||
else if (padHeld & PAD_BUTTON_DOWN && player.y < SCREEN_HEIGHT - player.h)
|
||||
{
|
||||
player.y += PLAYER_SPEED * deltaTime;
|
||||
}
|
||||
|
||||
if (ball.x < 0 || ball.x > SCREEN_WIDTH - ball.w)
|
||||
{
|
||||
ballVelocityX *= -1;
|
||||
|
||||
colorIndex = rand_range(0, 5);
|
||||
}
|
||||
|
||||
else if (ball.y < 0 || ball.y > SCREEN_HEIGHT - ball.h)
|
||||
{
|
||||
ballVelocityY *= -1;
|
||||
|
||||
colorIndex = rand_range(0, 5);
|
||||
}
|
||||
|
||||
else if (SDL_HasIntersection(&player, &ball))
|
||||
{
|
||||
ballVelocityX *= -1;
|
||||
ballVelocityY *= -1;
|
||||
|
||||
colorIndex = rand_range(0, 5);
|
||||
}
|
||||
|
||||
ball.x += ballVelocityX * deltaTime;
|
||||
ball.y += ballVelocityY * deltaTime;
|
||||
}
|
||||
|
||||
void render()
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
|
||||
SDL_RenderFillRect(renderer, &player);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, colors[colorIndex].r, colors[colorIndex].g, colors[colorIndex].b, 255);
|
||||
|
||||
SDL_RenderFillRect(renderer, &ball);
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
window = SDL_CreateWindow("My Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
|
||||
if (startSDL(window, renderer) > 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
Uint32 previousFrameTime = SDL_GetTicks();
|
||||
Uint32 currentFrameTime = previousFrameTime;
|
||||
float deltaTime = 0.0f;
|
||||
|
||||
PAD_Init();
|
||||
|
||||
while (true)
|
||||
{
|
||||
PAD_ScanPads();
|
||||
|
||||
currentFrameTime = SDL_GetTicks();
|
||||
deltaTime = (currentFrameTime - previousFrameTime) / 1000.0f;
|
||||
previousFrameTime = currentFrameTime;
|
||||
|
||||
handleEvents();
|
||||
update(deltaTime);
|
||||
render();
|
||||
}
|
||||
}
|
||||
71
src2/sdl_assets_loader.cpp
Normal file
71
src2/sdl_assets_loader.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
#include "sdl_assets_loader.h"
|
||||
|
||||
Sprite loadSprite(SDL_Renderer *renderer, const char *filePath, int positionX, int positionY)
|
||||
{
|
||||
SDL_Rect textureBounds = {positionX, positionY, 0, 0};
|
||||
|
||||
SDL_Texture *texture = IMG_LoadTexture(renderer, filePath);
|
||||
|
||||
if (texture != nullptr)
|
||||
{
|
||||
SDL_QueryTexture(texture, NULL, NULL, &textureBounds.w, &textureBounds.h);
|
||||
}
|
||||
|
||||
Sprite sprite = {texture, textureBounds};
|
||||
|
||||
return sprite;
|
||||
}
|
||||
|
||||
Mix_Chunk *loadSound(const char *filePath)
|
||||
{
|
||||
Mix_Chunk *sound = nullptr;
|
||||
|
||||
sound = Mix_LoadWAV(filePath);
|
||||
if (sound == nullptr)
|
||||
{
|
||||
printf("Failed to load scratch sound effect! SDL_mixer Error: %s\n", Mix_GetError());
|
||||
}
|
||||
|
||||
return sound;
|
||||
}
|
||||
|
||||
Mix_Music *loadMusic(const char *filePath)
|
||||
{
|
||||
Mix_Music *music = nullptr;
|
||||
|
||||
music = Mix_LoadMUS(filePath);
|
||||
if (music == nullptr)
|
||||
{
|
||||
printf("Failed to load music! SDL_mixer Error: %s\n", Mix_GetError());
|
||||
}
|
||||
|
||||
return music;
|
||||
}
|
||||
|
||||
void updateTextureText(SDL_Texture *&texture, const char *text, TTF_Font *&fontSquare, SDL_Renderer *renderer)
|
||||
{
|
||||
SDL_Color fontColor = {255, 255, 255};
|
||||
|
||||
if (fontSquare == nullptr)
|
||||
{
|
||||
printf("TTF_OpenFont fontSquare: %s\n", TTF_GetError());
|
||||
}
|
||||
|
||||
SDL_Surface *surface = TTF_RenderUTF8_Blended(fontSquare, text, fontColor);
|
||||
if (surface == nullptr)
|
||||
{
|
||||
printf("TTF_OpenFont: %s\n", TTF_GetError());
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Unable to create text surface! SDL Error: %s\n", SDL_GetError());
|
||||
exit(3);
|
||||
}
|
||||
|
||||
SDL_DestroyTexture(texture);
|
||||
texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
if (texture == nullptr)
|
||||
{
|
||||
printf("TTF_OpenFont: %s\n", TTF_GetError());
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Unable to create texture from surface! SDL Error: %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
SDL_FreeSurface(surface);
|
||||
}
|
||||
21
src2/sdl_assets_loader.h
Normal file
21
src2/sdl_assets_loader.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_mixer.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <iostream>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
SDL_Texture *texture;
|
||||
SDL_Rect textureBounds;
|
||||
} Sprite;
|
||||
|
||||
Sprite loadSprite(SDL_Renderer *renderer, const char *filePath, int positionX, int positionY);
|
||||
|
||||
Mix_Chunk *loadSound(const char *filePath);
|
||||
|
||||
Mix_Music *loadMusic(const char *filePath);
|
||||
|
||||
void updateTextureText(SDL_Texture *&texture, const char *text, TTF_Font *&fontSquare, SDL_Renderer *renderer);
|
||||
48
src2/sdl_starter.cpp
Normal file
48
src2/sdl_starter.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "sdl_starter.h"
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_mixer.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <iostream>
|
||||
|
||||
int startSDL(SDL_Window *window, SDL_Renderer *renderer) {
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0)
|
||||
{
|
||||
std::cout << "SDL crashed. Error: " << SDL_GetError();
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (window == nullptr)
|
||||
{
|
||||
std::cerr << "Failed to create window: " << SDL_GetError() << std::endl;
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (renderer == nullptr)
|
||||
{
|
||||
std::cerr << "Failed to create renderer: " << SDL_GetError() << std::endl;
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!IMG_Init(IMG_INIT_PNG))
|
||||
{
|
||||
std::cout << "SDL_image crashed. Error: " << SDL_GetError();
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
|
||||
{
|
||||
printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (TTF_Init() == -1)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
8
src2/sdl_starter.h
Normal file
8
src2/sdl_starter.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
const int SCREEN_WIDTH = 640;
|
||||
const int SCREEN_HEIGHT = 480;
|
||||
|
||||
int startSDL(SDL_Window *window, SDL_Renderer *renderer);
|
||||
Reference in New Issue
Block a user