GUI
Renderer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 by Maciej Wiecierzewski
3  */
4 
5 #include "Renderer.h"
6 
7 #include <allegro5/allegro_primitives.h>
8 
9 using namespace gui;
10 
12  font(NULL),
13  buffer(NULL),
14  targetBitmap(NULL)
15 {
16 }
17 
19 {
20 }
21 
22 void Renderer::setFont(ALLEGRO_FONT *font)
23 {
24  this->font = font;
25 }
26 
27 void Renderer::setBuffer(ALLEGRO_BITMAP *bitmap)
28 {
29  buffer = bitmap;
30 }
31 
32 Color Renderer::color(unsigned char r, unsigned char g, unsigned char b)
33 {
34  return al_map_rgb(r, g, b);
35 }
36 
38 {
39  if(targetBitmap != NULL)
40  {
41  al_destroy_bitmap(targetBitmap);
42  targetBitmap = NULL;
43  }
44 
45  al_set_target_bitmap(buffer);
46 }
47 
48 void Renderer::setTarget(int x, int y, int w, int h)
49 {
50  if(targetBitmap != NULL)
51  {
52  al_destroy_bitmap(targetBitmap);
53  targetBitmap = NULL;
54  }
55 
56  targetBitmap = al_create_sub_bitmap(buffer, x, y, w, h);
57  al_set_target_bitmap(targetBitmap);
58 }
59 
61 {
62  al_clear_to_color(al_map_rgb(255, 255, 255));
63 }
64 
65 void Renderer::drawFilledRect(int x1, int y1, int x2, int y2, Color color)
66 {
67  al_draw_filled_rectangle(x1, y1, x2, y2, color);
68 }
69 
70 void Renderer::drawRect(int x1, int y1, int x2, int y2, Color color, int thickness)
71 {
72  al_draw_rectangle(x1, y1, x2, y2, color, thickness);
73 }
74 
75 void Renderer::drawText(int x, int y, std::string text, Color color)
76 {
77  al_draw_text(font, color, x, y, 0, text.c_str());
78 }
79 
80 void Renderer::drawMultilineText(int x, int y, int maxWidth, float lineHeight, std::string text, Color color)
81 {
82  al_draw_multiline_text(font, color, x, y, maxWidth, lineHeight, 0, text.c_str());
83 }
84 
85 void Renderer::drawLine(int x1, int y1, int x2, int y2, Color color, int thickness)
86 {
87  al_draw_line(x1, y1, x2, y2, color, thickness);
88 }
89 
90 int Renderer::getTextWidth(std::string text)
91 {
92  return al_get_text_width(font, text.c_str());
93 }
94 
96 {
97  return al_get_font_line_height(font);
98 }