-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNSurface.cpp
More file actions
executable file
·84 lines (67 loc) · 3.09 KB
/
NSurface.cpp
File metadata and controls
executable file
·84 lines (67 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// NSurface.cpp
// Third
//
// Created by Didrik Munther on 12/03/15.
// Copyright (c) 2015 Didrik Munther. All rights reserved.
//
#include "NSurface.h"
#include "CSpriteSheet.h"
#include "CSprite.h"
#include "CText.h"
#include "CWindow.h"
#include "CCamera.h"
Triangle Triangle::normalizeWithCamera(CCamera* camera) {
return Triangle{a.normalizeWithCamera(camera), b.normalizeWithCamera(camera), c.normalizeWithCamera(camera)};
}
Line Line::normalizeWithCamera(CCamera* camera) {
return Line{x - camera->offsetX(), y - camera->offsetY(), x2 - camera->offsetX(), y2 - camera->offsetY(), color};
}
void NSurface::renderRect(int x, int y, int w, int h, CWindow* window, int r, int g, int b, int a /* = 255 */) {
SDL_Rect rect{x, y, w, h};
SDL_SetRenderDrawColor(window->getRenderer(), r, g, b, a);
SDL_RenderFillRect(window->getRenderer(), &rect);
}
void NSurface::renderSprite(int x, int y, int w, int h, CSprite *sprite, CWindow *window, SDL_RendererFlip flip, int a /* = 255 */) {
if(sprite == nullptr) return;
SDL_Rect src = *sprite->getSource();
SDL_Rect destination = {x, y, w, h};
SDL_SetTextureAlphaMod(sprite->getSpriteSheet()->getTexture(), a);
SDL_RenderCopyEx(window->getRenderer(), sprite->getSpriteSheet()->getTexture(), &src, &destination, 0, nullptr, flip);
}
void NSurface::renderText(int x, int y, CText* text, CWindow* window, float size /* = 1 */) {
SDL_Surface *surface = TTF_RenderText_Blended(text->getFont(), (*text->getText()).c_str(), *text->getColor());
SDL_Texture *texture = SDL_CreateTextureFromSurface(window->getRenderer(), surface);
int w, h;
SDL_QueryTexture(texture, nullptr, nullptr, &w, &h);
renderTexture(x, y, (float)w * size, (float)h * size, window->getRenderer(), texture);
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
}
void NSurface::renderTexture(int x, int y, int w, int h, SDL_Renderer* renderer, SDL_Texture *texture) {
SDL_Rect destination = {x, y, w, h};
SDL_RenderCopy(renderer, texture, nullptr, &destination);
}
void NSurface::renderTexture(Box destination, SDL_Rect src, SDL_Renderer* renderer, SDL_Texture* texture, SDL_RendererFlip flip, int angle, int a /* = 255 */) {
if(!texture || !renderer)
return;
SDL_SetTextureAlphaMod(texture, a);
SDL_Rect dest = destination;
SDL_RenderCopyEx(renderer, texture, &src, &dest, angle, nullptr, flip);
}
void NSurface::renderLine(Line line, SDL_Renderer* renderer, CCamera* camera /* = nullptr */) {
if(camera) {
line = line.normalizeWithCamera(camera);
// if(!camera->collision(line.x, line.y, line.x2, line.y2))
// return;
}
SDL_SetRenderDrawColor(renderer, line.color.r, line.color.g, line.color.b, line.color.a);
SDL_RenderDrawLine(renderer, line.x, line.y, line.x2, line.y2);
}
void NSurface::renderTriangle(Triangle triangle, SDL_Renderer* renderer, CCamera* camera /* = nullptr */) {
if(camera)
triangle = triangle.normalizeWithCamera(camera);
renderLine(triangle.a, renderer);
renderLine(triangle.b, renderer);
renderLine(triangle.c, renderer);
}