"Improved"

This commit is contained in:
2026-02-04 15:54:09 -06:00
parent ad13d6c6a1
commit 708c4d0ec3
15 changed files with 150 additions and 154 deletions

View File

@@ -16,9 +16,9 @@ target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
# Main Binary Source
target_sources(${DUSK_BINARY_TARGET_NAME}
PRIVATE
main.cpp
sdl_starter.cpp
sdl_assets_loader.cpp
main.c
sdl_starter.c
sdl_assets_loader.c
)
# Defs

View File

@@ -2,8 +2,8 @@
#include "sdl_starter.h"
#include "sdl_assets_loader.h"
SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
const int PLAYER_SPEED = 600;

10
src2/null.c Normal file
View File

@@ -0,0 +1,10 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
void dusk_null_method(void) {
return;
}

View File

@@ -6,7 +6,7 @@ Sprite loadSprite(SDL_Renderer *renderer, const char *filePath, int positionX, i
SDL_Texture *texture = IMG_LoadTexture(renderer, filePath);
if (texture != nullptr)
if (texture != NULL)
{
SDL_QueryTexture(texture, NULL, NULL, &textureBounds.w, &textureBounds.h);
}
@@ -16,12 +16,13 @@ Sprite loadSprite(SDL_Renderer *renderer, const char *filePath, int positionX, i
return sprite;
}
Mix_Chunk *loadSound(const char *filePath)
{
Mix_Chunk *sound = nullptr;
Mix_Chunk *sound = NULL;
sound = Mix_LoadWAV(filePath);
if (sound == nullptr)
if (sound == NULL)
{
printf("Failed to load scratch sound effect! SDL_mixer Error: %s\n", Mix_GetError());
}
@@ -29,12 +30,13 @@ Mix_Chunk *loadSound(const char *filePath)
return sound;
}
Mix_Music *loadMusic(const char *filePath)
{
Mix_Music *music = nullptr;
Mix_Music *music = NULL;
music = Mix_LoadMUS(filePath);
if (music == nullptr)
if (music == NULL)
{
printf("Failed to load music! SDL_mixer Error: %s\n", Mix_GetError());
}
@@ -42,26 +44,27 @@ Mix_Music *loadMusic(const char *filePath)
return music;
}
void updateTextureText(SDL_Texture *&texture, const char *text, TTF_Font *&fontSquare, SDL_Renderer *renderer)
void updateTextureText(SDL_Texture **texture, const char *text, TTF_Font **fontSquare, SDL_Renderer *renderer)
{
SDL_Color fontColor = {255, 255, 255};
if (fontSquare == nullptr)
if (*fontSquare == NULL)
{
printf("TTF_OpenFont fontSquare: %s\n", TTF_GetError());
}
SDL_Surface *surface = TTF_RenderUTF8_Blended(fontSquare, text, fontColor);
if (surface == nullptr)
SDL_Surface *surface = TTF_RenderUTF8_Blended(*fontSquare, text, fontColor);
if (surface == NULL)
{
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)
SDL_DestroyTexture(*texture);
*texture = SDL_CreateTextureFromSurface(renderer, surface);
if (*texture == NULL)
{
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());

View File

@@ -4,7 +4,6 @@
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
typedef struct
{
@@ -18,4 +17,4 @@ 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);
void updateTextureText(SDL_Texture **texture, const char *text, TTF_Font **fontSquare, SDL_Renderer *renderer);

View File

@@ -1,44 +1,51 @@
#include "sdl_starter.h"
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include <stdio.h>
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();
printf("SDL crashed. Error: %s\n", SDL_GetError());
return 1;
}
if (window == nullptr)
if (window == NULL)
{
std::cerr << "Failed to create window: " << SDL_GetError() << std::endl;
fprintf(stderr, "Failed to create window: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
if (renderer == nullptr)
if (renderer == NULL)
{
std::cerr << "Failed to create renderer: " << SDL_GetError() << std::endl;
fprintf(stderr, "Failed to create renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
if (!IMG_Init(IMG_INIT_PNG))
{
std::cout << "SDL_image crashed. Error: " << SDL_GetError();
printf("SDL_image crashed. Error: %s\n", 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;

View File

@@ -1,8 +1,15 @@
#pragma once
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <SDL2/SDL.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
int startSDL(SDL_Window *window, SDL_Renderer *renderer);