Skip to content

Commit

Permalink
Base code ready
Browse files Browse the repository at this point in the history
  • Loading branch information
TanishTuteja committed Apr 5, 2022
1 parent 0502255 commit 7b0fad1
Show file tree
Hide file tree
Showing 26 changed files with 3,856 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
*.out
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#CC specifies which compiler we're using
CXX = g++

#OBJS specifies which files to compile as part of the project
OBJS = collision.cpp main.cpp MyTexture.cpp MyWindow.cpp Player.cpp Tile.cpp TileAtlas.cpp

#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -g

#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lSDL2 -lSDL2_image # -lSDL2_ttf -lSDL2_mixer

#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = experiment.out

#This is the target that compiles our executable
all : $(OBJS)
$(CXX) -I/usr/include/SDL2 $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
149 changes: 149 additions & 0 deletions MyTexture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#include "MyTexture.h"
#include <SDL.h>
#include <SDL_image.h>
#include <string>
#include <iostream>

LTexture::LTexture()
{
// Initialize
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}

LTexture::~LTexture()
{
// Deallocate
free();
}

bool LTexture::loadFromFile(SDL_Renderer *renderer, std::string path)
{
// Get rid of preexisting texture
free();

// The final texture
SDL_Texture *newTexture = NULL;

// Load image at specified path
SDL_Surface *loadedSurface = IMG_Load(path.c_str());
if (loadedSurface == NULL)
{
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
}
else
{
// Color key image
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0, 0xFF, 0xFF));

// Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if (newTexture == NULL)
{
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
else
{
// Get image dimensions
mWidth = loadedSurface->w;
mHeight = loadedSurface->h;
}

// Get rid of old loaded surface
SDL_FreeSurface(loadedSurface);
}

// Return success
mTexture = newTexture;
return mTexture != NULL;
}

#if defined(SDL_TTF_MAJOR_VERSION)
bool LTexture::loadFromRenderedText(std::string textureText, SDL_Color textColor)
{
// Get rid of preexisting texture
free();

// Render text surface
SDL_Surface *textSurface = TTF_RenderText_Solid(gFont, textureText.c_str(), textColor);
if (textSurface != NULL)
{
// Create texture from surface pixels
mTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
if (mTexture == NULL)
{
printf("Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError());
}
else
{
// Get image dimensions
mWidth = textSurface->w;
mHeight = textSurface->h;
}

// Get rid of old surface
SDL_FreeSurface(textSurface);
}
else
{
printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());
}

// Return success
return mTexture != NULL;
}
#endif

void LTexture::free()
{
// Free texture if it exists
if (mTexture != NULL)
{
SDL_DestroyTexture(mTexture);
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
}

void LTexture::setColor(Uint8 red, Uint8 green, Uint8 blue)
{
// Modulate texture rgb
SDL_SetTextureColorMod(mTexture, red, green, blue);
}

void LTexture::setBlendMode(SDL_BlendMode blending)
{
// Set blending function
SDL_SetTextureBlendMode(mTexture, blending);
}

void LTexture::setAlpha(Uint8 alpha)
{
// Modulate texture alpha
SDL_SetTextureAlphaMod(mTexture, alpha);
}

void LTexture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, double angle, SDL_Point *center, SDL_RendererFlip flip)
{
// Set rendering space and render to screen
SDL_Rect renderQuad = {x, y, mWidth, mHeight};
if (clip != NULL)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
// Render to screen
SDL_RenderCopyEx(renderer, mTexture, clip, &renderQuad, angle, center, flip);
}

int LTexture::getWidth()
{
return mWidth;
}

int LTexture::getHeight()
{
return mHeight;
}
53 changes: 53 additions & 0 deletions MyTexture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef TEXTURE_H
#define TEXTURE_H

#include <SDL.h>
#include <SDL_image.h>
#include <string>

class LTexture
{
public:
// Initializes variables
LTexture();

// Deallocates memory
~LTexture();

// Loads image at specified path
bool loadFromFile(SDL_Renderer *renderer, std::string path);

#if defined(SDL_TTF_MAJOR_VERSION)
// Creates image from font string
bool loadFromRenderedText(std::string textureText, SDL_Color textColor);
#endif

// Deallocates texture
void free();

// Set color modulation
void setColor(Uint8 red, Uint8 green, Uint8 blue);

// Set blending
void setBlendMode(SDL_BlendMode blending);

// Set alpha modulation
void setAlpha(Uint8 alpha);

// Renders texture at given point
void render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip = NULL, double angle = 0.0, SDL_Point *center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE);

// Gets image dimensions
int getWidth();
int getHeight();

private:
// The actual hardware texture
SDL_Texture *mTexture;

// Image dimensions
int mWidth;
int mHeight;
};

#endif
Loading

0 comments on commit 7b0fad1

Please sign in to comment.