forked from zombietype/N-Body-Simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.cpp
More file actions
77 lines (65 loc) · 1.93 KB
/
Graphics.cpp
File metadata and controls
77 lines (65 loc) · 1.93 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
//
// Graphics.cpp
// nbody_bh
//
// Created by Anish Bhobe on 6/29/17.
// Copyright © 2017 Anish Bhobe. All rights reserved.
//
#include "Graphics.hpp"
#include <iostream>
#include <allegro5/allegro_primitives.h>
Celestial::Graphics::Graphics(float span, int screenSide):screenSide(screenSide),scale(screenSide/span),offset(screenSide/2) {
if(!al_init()){
std::cerr << "ERROR: Allegro could not be initialized." << std::endl;
exit(1);
}
else {
std::cerr << "Allegro initialized." << std::endl;
}
if((display = al_create_display(screenSide, screenSide)) == nullptr){
std::cerr << "ERROR: Display could not be created" << std::endl;
exit(1);
}
else {
std::cerr << "Display (" << screenSide << "x" << screenSide <<")created." << std::endl;
}
White = al_map_rgb(255, 255, 255);
Red = al_map_rgb(255, 0, 0);
}
void Celestial::Graphics::DrawPoint(double x, double y) {
x *= scale;
y *= scale;
x += offset;
y += offset;
if(x > 0 && y > 0)
al_draw_pixel(x, y, White);
}
void Celestial::Graphics::DrawPoint(const Body& bd){
DrawPoint(bd.position[0] , bd.position[1]);
}
void Celestial::Graphics::DrawLine(double x1, double y1, double x2, double y2) {
x1+=offset;
x2+=offset;
y1+=offset;
y2+=offset;
if(x1>0 && x2 >0 && y1>0 && y2>0)
al_draw_line(x1, y1, x2, y2, White, 1);
}
void Celestial::Graphics::DrawQuad(double x, double y, double side){
x*=scale;
y*=scale;
side*=scale;
x+= offset;
y+= offset;
if(x+y > side)
al_draw_rectangle(x - side/2, y - side/2, x + side/2, y + side/2, al_map_rgb(255,255,255), 1);
}
void Celestial::Graphics::DrawQuad(const Quad& toDrawQuad){
DrawQuad(toDrawQuad.center[0], toDrawQuad.center[1], toDrawQuad.side);
}
void Celestial::Graphics::Draw() {
al_flip_display();
}
Celestial::Graphics::~Graphics(){
al_destroy_display(display);
}