First push

This commit is contained in:
Wizzard 2023-12-12 16:13:36 -05:00
commit 65ae6a78f0
5 changed files with 180 additions and 0 deletions

26
CMakeLists.txt Normal file
View File

@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.10)
project(CDoom)
set(CMAKE_CXX_STANDARD 17)
# Include directory for header files
include_directories(${PROJECT_SOURCE_DIR}/src/include)
# Automatically find all .cpp files in src/ and subdirectories
file(GLOB_RECURSE SOURCES "src/*.cpp")
# Find SDL2 library
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
# Find SDL2_ttf library
find_package(SDL2_ttf REQUIRED)
include_directories(${SDL2_TTF_INCLUDE_DIRS})
set(SDL2_TTF_LIBRARIES "/usr/lib/libSDL2_ttf.so")
# Create executable with all source files
add_executable(CDoom ${SOURCES})
# Link SDL2 and SDL2_ttf libraries
target_link_libraries(CDoom ${SDL2_LIBRARIES} ${SDL2_TTF_LIBRARIES})

25
src/WadFile.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "WadFile.h"
#include <iostream>
#include <fstream>
WadFile::WadFile(const std::string& filename) : filename(filename) {
}
WadFile::~WadFile() {
}
bool WadFile::Load() {
std::ifstream file(filename, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Failed to open WAD file: " << filename << std::endl;
return false;
}
return true;
}
SDL_Texture* WadFile::GetShotgunTexture(SDL_Renderer* renderer) {
return nullptr; // Placeholder
}

0
src/include/Renderer.h Normal file
View File

30
src/include/WadFile.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef WADFILE_H
#define WADFILE_H
#include <SDL.h>
#include <string>
#include <vector>
struct WadHeader {
};
struct LumpDirectory {
};
class WadFile {
public:
WadFile(const std::string& filename);
~WadFile();
bool Load();
SDL_Texture* GetShotgunTexture(SDL_Renderer* renderer);
private:
std::string filename;
std::vector<LumpDirectory> lumps;
};
#endif // WADFILE_H

99
src/main.cpp Normal file
View File

@ -0,0 +1,99 @@
#include <SDL.h>
#include <SDL_ttf.h>
#include <iostream>
#include <string>
#include "WadFile.h"
#define BUILD_DATE __DATE__ " " __TIME__
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
return 1;
}
if (TTF_Init() == -1) {
std::cerr << "TTF could not initialize! TTF_Error: " << TTF_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_Window* window = SDL_CreateWindow("CDoom", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
if (!window) {
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
TTF_Quit();
SDL_Quit();
return 1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 1;
}
WadFile wad("DOOM2.WAD");
if (!wad.Load()) {
std::cerr << "Failed to load WAD file" << std::endl;
}
SDL_Texture* shotgunTexture = wad.GetShotgunTexture(renderer);
if (!shotgunTexture) {
std::cerr << "Failed to load shotgun texture" << std::endl;
}
TTF_Font* font = TTF_OpenFont("OpenSans-Bold.ttf", 24);
if (!font) {
std::cerr << "Failed to load font! TTF_Error: " << TTF_GetError() << std::endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 1;
}
SDL_Color textColor = {255, 255, 255, 255};
SDL_Surface* textSurface = TTF_RenderText_Solid(font, ("CDoom: Built on " + std::string(BUILD_DATE)).c_str(), textColor);
if (!textSurface) {
std::cerr << "Failed to create text surface! TTF_Error: " << TTF_GetError() << std::endl;
TTF_CloseFont(font);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 1;
}
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Rect textRect = {0, 0, textSurface->w, textSurface->h};
SDL_FreeSurface(textSurface);
bool running = true;
SDL_Event event;
while (running) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
running = false;
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(textTexture);
TTF_CloseFont(font);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
return 0;
}