Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • et2-stoker/assignment2020resit
1 result
Show changes
Showing
with 581 additions and 0 deletions
/**
* @file
* @author Benedict R. Gaster
*/
#pragma once
#include <events.hpp>
#include <context.hpp>
namespace uwe {
class App {
private:
Context context_;
bool output_framerate_;
public:
App();
~App();
void init(int width, int height, std::string title);
void run();
/// toggles output framerate to console
void toggle_framerate() {
output_framerate_ = !output_framerate_;
}
virtual void begin() = 0;
virtual void update() = 0;
virtual void draw() = 0;
virtual void key_pressed(Scancode scancode, bool is_repeat) {
};
virtual void key_released(Scancode scancode) {
};
virtual void mouse_pressed(int x, int y, Button button) {
};
virtual void mouse_released(int x, int y, Button button) {
};
virtual void mouse_moved(int x, int y) {
};
// API
int get_framebuffer_width() const {
return context_.get_tex_width();
}
int get_framebuffer_height() const {
return context_.get_tex_height();
}
uint8_t& framebuffer(size_t offset) {
return context_.pixel(offset);
}
void set_framebuffer_non_scaled(size_t offset, Colour colour) {
context_.set_pixel(offset, colour);
}
void set_framebuffer(int x, int y, Colour colour) {
int offset = x*sizeof(uwe::Colour) + get_framebuffer_width() * sizeof(uwe::Colour) * y;
context_.set_pixel(offset, colour);
}
void clear_framebuffer(Colour colour) {
for (int offset = 0; offset < get_framebuffer_width() * get_framebuffer_height() * 4; offset++) {
context_.set_pixel(offset, colour);
}
}
void blit_framebuffer() {
// copy framebuffer pixels to GPU texture for rendering
SDL_UpdateTexture(
context_.get_texture(),
NULL,
context_.get_pixels().data(),
context_.get_tex_width() * 4);
SDL_RenderCopy( context_.get_renderer(), context_.get_texture(), NULL, NULL );
}
void clear(Colour colour) {
SDL_SetRenderDrawColor(
context_.get_renderer(),
colour.red_,
colour.green_,
colour.blue_,
colour.alpha_ );
SDL_RenderClear( context_.get_renderer() );
}
Font create_font(std::string path, int point_size, Colour colour) {
return context_.create_font(path, point_size, colour);
}
void draw_font(Font font, std::string msg, float x, float y) {
context_.draw_font(font, msg, x, y);
}
Image create_image(std::string path) {
return context_.create_image(path);
}
Image create_image(int image_width, int image_height) {
return context_.create_image(image_width, image_height);
}
void get_image_size(Image image, int *width, int *height) {
context_.get_image_size(image, width, height);
}
void draw_image(Image image, Rect src, Rect dest) {
context_.draw_image(image, src, dest);
}
void set_renderer_taret(Image image, bool clear) {
SDL_SetRenderTarget(context_.get_renderer(), image);
if (clear) {
SDL_RenderClear(context_.get_renderer());
}
}
// drawing
void set_draw_color(Colour colour) {
SDL_SetRenderDrawColor(
context_.get_renderer(),
colour.red_,
colour.green_,
colour.blue_,
colour.alpha_ );
}
void draw_rect(int x, int y, int width, int height) {
Rect rect = make_rect(x, y, width, height);
SDL_RenderDrawRect(context_.get_renderer(), &rect);
}
void draw_rect(Rect rect) {
SDL_RenderDrawRect(context_.get_renderer(), &rect);
}
void draw_rect_fill(int x, int y, int width, int height) {
Rect rect = make_rect(x, y, width, height);
SDL_RenderFillRect(context_.get_renderer(), &rect);
}
void draw_rect_fill(Rect rect) {
SDL_RenderFillRect(context_.get_renderer(), &rect);
}
void draw_line(int x1, int y1, int x2, int y2) {
SDL_RenderDrawLine(context_.get_renderer(), x1, y1, x2, y2);
}
void draw_point(int x, int y) {
SDL_RenderDrawPoint(context_.get_renderer(), x, y);
}
};
void run();
} // namespace uwe
\ No newline at end of file
#ifndef _BOARD_
#define _BOARD_
class Board
{
public:
// Decleration of the board values
enum BOARD_PIECE {EMPTY, PX, PO};
Board();
// A function to clear the board
void clearb();
// Boolean values to check wether a space is already occupied by either an X or O
bool fieldEmpty(int row, int col) const;
bool fieldX(int row, int col) const;
bool fieldO(int row, int col) const;
// Stores the board values
BOARD_PIECE m[3][3];
// A counter for how many moves have been made so that players can draw
int takenPlace;
// Checks to decide wether or not someone has won
bool checkRow() const;
bool checkCol() const;
bool checkDiag() const;
};
#endif
\ No newline at end of file
/**
* @file
* @author Benedict R. Gaster
*/
#pragma once
#include <vector>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL_FontCache.h>
#include <events.hpp>
namespace uwe {
class Colour {
public:
uint8_t blue_;
uint8_t green_;
uint8_t red_;
uint8_t alpha_;
Colour(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha):
red_{red},
green_{green},
blue_{blue},
alpha_{alpha} {
}
Colour(const Colour& colour) :
red_{colour.red_},
green_{colour.green_},
blue_{colour.blue_},
alpha_{colour.alpha_} { }
static Colour red() {
return Colour{ 255, 0, 0, SDL_ALPHA_OPAQUE };
}
static Colour green() {
return Colour{ 0, 255, 0, SDL_ALPHA_OPAQUE };
}
static Colour blue() {
return Colour{ 0, 0, 255, SDL_ALPHA_OPAQUE };
}
static Colour white() {
return Colour{ 255, 255, 255, SDL_ALPHA_OPAQUE };
}
static Colour black() {
return Colour{ 0, 0, 0, SDL_ALPHA_OPAQUE };
}
};
inline Colour make_colour(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) {
return Colour{red, green, blue, alpha};
}
typedef SDL_Rect Rect;
inline Rect make_rect(int x, int y, int width, int height) {
Rect rect;
rect.x = x;
rect.y = y;
rect.w = width;
rect.h = height;
return rect;
}
typedef SDL_Texture* Image;
typedef FC_Font* Font;
class Context {
public:
/** @brief Create a context
* @param width an integer setting the window width
* @param height an integer setting the window height
*/
Context();
/// Destory context
~Context();
void init(int width, int height, std::string title);
/// Output details of GPU backend and texture formats
void dump_renderer_info();
/// run the control loop, returns only when user exits
//void run(App* app);
int get_height() const {
return height_;
}
int get_width() const {
return width_;
}
SDL_Window* get_window() const {
return window_;
}
SDL_Renderer* get_renderer() const {
return renderer_;
}
SDL_Texture* get_texture() const {
return texture_;
}
std::vector< uint8_t >& get_pixels() {
return pixels_;
}
uint8_t& pixel(size_t offset) {
return pixels_[offset];
}
void set_pixel(size_t offset, Colour colour) {
pixels_[offset + 0] = colour.blue_;
pixels_[offset + 1] = colour.green_;
pixels_[offset + 2] = colour.red_;
pixels_[offset + 3] = colour.alpha_;
}
int get_tex_width() const {
return tex_width_;
}
int get_tex_height() const {
return tex_height_;
}
Image create_image(std::string path) {
SDL_Surface* surface = IMG_Load(path.c_str());
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer_, surface);
SDL_FreeSurface(surface);
return texture;
}
Image create_image(int tex_width, int tex_height) {
return SDL_CreateTexture(
renderer_,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
tex_width,
tex_height);
}
void delete_image(Image image) {
SDL_DestroyTexture(image);
}
void get_image_size(Image image, int *width, int *height) {
uint32_t format;
int access;
SDL_QueryTexture(image, &format, &access, width, height);
}
void draw_image(Image image, Rect src, Rect dest) {
SDL_RenderCopy(renderer_, image, &src, &dest);
}
Font create_font(std::string path, int point_size, Colour colour) {
auto font = FC_CreateFont();
FC_LoadFont(
font,
renderer_,
path.c_str(),
point_size,
FC_MakeColor(colour.red_, colour.green_, colour.blue_, colour.alpha_),
TTF_STYLE_NORMAL);
loaded_fonts_.push_back(font);
return font;
}
void draw_font(Font font, std::string msg, float x, float y) {
FC_Draw(font, renderer_, x, y, msg.c_str());
}
private:
int height_; ///< window height
int width_; ///< window width
int tex_width_; ///< texture width
int tex_height_; ///< texture height
SDL_Window* window_; ///< window handle
SDL_Renderer* renderer_; ///< graphics backend handle
SDL_Texture* texture_; ///< pixel buffer texture handle
std::vector< uint8_t > pixels_; ///< pixels representing framebuffer
std::vector<Font> loaded_fonts_;
};
}
/**
* @file
* @author Benedict R. Gaster
*/
#pragma once
#include <vector>
#include <SDL2/SDL.h>
namespace uwe {
// event types
enum class Event {
EVENT_QUIT = SDL_QUIT,
EVENT_KEYDOWN = SDL_KEYDOWN,
};
// scancodes
enum class Scancode {
D_0 = SDL_SCANCODE_0,
D_1 = SDL_SCANCODE_1,
D_2 = SDL_SCANCODE_2,
D_3 = SDL_SCANCODE_3,
D_4 = SDL_SCANCODE_4,
D_5 = SDL_SCANCODE_5,
D_6 = SDL_SCANCODE_6,
D_7 = SDL_SCANCODE_7,
D_8 = SDL_SCANCODE_8,
D_9 = SDL_SCANCODE_9,
A = SDL_SCANCODE_A,
B = SDL_SCANCODE_B,
C = SDL_SCANCODE_C,
D = SDL_SCANCODE_D,
E = SDL_SCANCODE_E,
F = SDL_SCANCODE_F,
G = SDL_SCANCODE_G,
H = SDL_SCANCODE_H,
I = SDL_SCANCODE_I,
J = SDL_SCANCODE_J,
K = SDL_SCANCODE_K,
L = SDL_SCANCODE_L,
M = SDL_SCANCODE_M,
N = SDL_SCANCODE_N,
O = SDL_SCANCODE_O,
P = SDL_SCANCODE_P,
Q = SDL_SCANCODE_Q,
R = SDL_SCANCODE_R,
S = SDL_SCANCODE_S,
T = SDL_SCANCODE_T,
U = SDL_SCANCODE_U,
V = SDL_SCANCODE_V,
W = SDL_SCANCODE_W,
X = SDL_SCANCODE_X,
Y = SDL_SCANCODE_Y,
Z = SDL_SCANCODE_Z,
LSHIFT = SDL_SCANCODE_LSHIFT,
RSHIFT = SDL_SCANCODE_RSHIFT,
RETURN = SDL_SCANCODE_RETURN,
BACKSPACE = SDL_SCANCODE_BACKSPACE,
SPACE = SDL_SCANCODE_SPACE,
UP = SDL_SCANCODE_UP,
DOWN = SDL_SCANCODE_DOWN,
LEFT = SDL_SCANCODE_LEFT,
RIGHT = SDL_SCANCODE_RIGHT,
};
enum class Button {
LEFT = SDL_BUTTON_LEFT,
RIGHT = SDL_BUTTON_RIGHT,
};
} // namespace uwe
\ No newline at end of file
/**
* @file
* @author Benedict R. Gaster
*/
#pragma once
#include <vector>
#include <SDL2/SDL.h>
#include <drawcontext.hpp>
namespace uwe {
// event types
enum class Event {
EVENT_QUIT = SDL_QUIT,
EVENT_KEYDOWN = SDL_KEYDOWN,
};
// scancodes
enum class Scancode {
SCANCODE_0 = SDL_SCANCODE_0,
SCANCODE_1 = SDL_SCANCODE_1,
SCANCODE_2 = SDL_SCANCODE_2,
SCANCODE_3 = SDL_SCANCODE_3,
SCANCODE_4 = SDL_SCANCODE_4,
SCANCODE_5 = SDL_SCANCODE_5,
SCANCODE_6 = SDL_SCANCODE_6,
SCANCODE_7 = SDL_SCANCODE_7,
SCANCODE_8 = SDL_SCANCODE_8,
SCANCODE_9 = SDL_SCANCODE_9,
SCANCODE_A = SDL_SCANCODE_A,
SCANCODE_B = SDL_SCANCODE_B,
SCANCODE_C = SDL_SCANCODE_C,
SCANCODE_D = SDL_SCANCODE_D,
SCANCODE_E = SDL_SCANCODE_E,
SCANCODE_F = SDL_SCANCODE_F,
SCANCODE_G = SDL_SCANCODE_G,
SCANCODE_H = SDL_SCANCODE_H,
SCANCODE_I = SDL_SCANCODE_I,
SCANCODE_J = SDL_SCANCODE_J,
SCANCODE_K = SDL_SCANCODE_K,
SCANCODE_L = SDL_SCANCODE_L,
SCANCODE_M = SDL_SCANCODE_M,
SCANCODE_N = SDL_SCANCODE_N,
SCANCODE_O = SDL_SCANCODE_O,
SCANCODE_P = SDL_SCANCODE_P,
SCANCODE_Q = SDL_SCANCODE_Q,
SCANCODE_R = SDL_SCANCODE_R,
SCANCODE_S = SDL_SCANCODE_S,
SCANCODE_T = SDL_SCANCODE_T,
SCANCODE_U = SDL_SCANCODE_U,
SCANCODE_V = SDL_SCANCODE_V,
SCANCODE_W = SDL_SCANCODE_W,
SCANCODE_X = SDL_SCANCODE_X,
SCANCODE_Y = SDL_SCANCODE_Y,
SCANCODE_Z = SDL_SCANCODE_Z,
SCANCODE_LSHIFT = SDL_SCANCODE_LSHIFT,
SCANCODE_RSHIFT = SDL_SCANCODE_RSHIFT,
SCANCODE_RETURN = SDL_SCANCODE_RETURN,
SCANCODE_BACKSPACE = SDL_SCANCODE_BACKSPACE,
SCANCODE_SPACE = SDL_SCANCODE_SPACE,
SCANCODE_UP = SDL_SCANCODE_UP,
SCANCODE_DOWN = SDL_SCANCODE_DOWN,
SCANCODE_LEFT = SDL_SCANCODE_LEFT,
SCANCODE_RIGHT = SDL_SCANCODE_RIGHT,
};
class Context {
public:
/** @brief Create a context
* @param width an integer setting the window width
* @param height an integer setting the window height
*/
Context(int width, int height);
/// Destory context
~Context();
/// Output details of GPU backend and texture formats
void dump_renderer_info();
/// run the control loop, returns only when user exits
void run();
private:
int height_; ///< window height
int width_; ///< window width
int tex_width_; ///< texture width
int tex_height_; ///< texture height
SDL_Window* window_; ///< window handle
SDL_Renderer* renderer_; ///< graphics backend handle
SDL_Texture* texture_; ///< pixel buffer texture handle
std::vector< uint8_t > pixels_; ///< pixels representing framebuffer
};
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.