mirror of
https://github.com/AquaMorph/Droplets.git
synced 2025-07-10 05:32:01 +00:00
Basic graph rendering system
This commit is contained in:
66
src/graphics/graph.cpp
Normal file
66
src/graphics/graph.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
#include "graph.h"
|
||||
|
||||
Graph::Graph(int m_width, int m_height) {
|
||||
width = m_width;
|
||||
height = m_height;
|
||||
graph = new bool*[width];
|
||||
for (int w = 0; w < width; w++) {
|
||||
graph[w] = new bool[height];
|
||||
for (int h = 0; h < height; h++) {
|
||||
graph[w][h] = false;
|
||||
}
|
||||
}
|
||||
active = 0;
|
||||
}
|
||||
|
||||
Graph::~Graph() {
|
||||
for (int w = 0; w < width; w++) {
|
||||
delete[] graph[w];
|
||||
}
|
||||
delete[] graph;
|
||||
}
|
||||
|
||||
int Graph::GetNextActive() {
|
||||
if (active == width - 1) {
|
||||
return 0;
|
||||
} else {
|
||||
return active+1;
|
||||
}
|
||||
}
|
||||
|
||||
void Graph::Update() {
|
||||
active = GetNextActive();
|
||||
ClearColumn();
|
||||
}
|
||||
|
||||
void Graph::ClearColumn() {
|
||||
for (int h = 0; h < height; h++) {
|
||||
graph[active][h] = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Graph::SetPixel(int pos) {
|
||||
SetPixel(pos, true);
|
||||
}
|
||||
|
||||
void Graph::SetPixel(int pos, bool on) {
|
||||
graph[active][pos] = on;
|
||||
}
|
||||
|
||||
void Graph::SetPixelPercentage(double percentage) {
|
||||
SetPixelPercentage(percentage, true);
|
||||
}
|
||||
|
||||
void Graph::SetPixelPercentage(double percentage, bool on) {
|
||||
SetPixel((int)(percentage*height), on);
|
||||
}
|
||||
|
||||
void Graph::Draw(DaisyPatch* patch, int x, int y) {
|
||||
for (int w = 0; w < width; w++) {
|
||||
for (int h = 0; h < height; h++) {
|
||||
patch->display.DrawPixel(x+w, y+h,
|
||||
graph[GetShiftArray(w, active, width)]
|
||||
[GetShiftArray(height-h, height-1, height)]);
|
||||
}
|
||||
}
|
||||
}
|
29
src/graphics/graph.h
Normal file
29
src/graphics/graph.h
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef DROPLETS_GRAPHICS_GRAPH_H
|
||||
#define DROPLETS_GRAPHICS_GRAPH_H
|
||||
|
||||
#include "daisy_patch.h"
|
||||
#include "../util.h"
|
||||
|
||||
using namespace daisy;
|
||||
|
||||
class Graph {
|
||||
private:
|
||||
int width, height, active;
|
||||
bool** graph;
|
||||
int GetNextActive();
|
||||
public:
|
||||
Graph(int m_width,
|
||||
int m_height);
|
||||
~Graph();
|
||||
void Update();
|
||||
void ClearColumn();
|
||||
void SetPixel(int pos);
|
||||
void SetPixel(int pos, bool on);
|
||||
void SetPixelPercentage(double percentage);
|
||||
void SetPixelPercentage(double percentage, bool on);
|
||||
void Draw(DaisyPatch* patch, int x, int y);
|
||||
};
|
||||
|
||||
#endif // DROPLETS_GRAPHICS_GRAPH_H
|
@ -116,12 +116,6 @@ void Sprite::AdjustYShift(int y) {
|
||||
y_shift += y;
|
||||
}
|
||||
|
||||
int Sprite::GetShiftArray(int pos,
|
||||
int shift,
|
||||
int array_size) {
|
||||
return (array_size + ((pos + shift) % array_size)) % array_size;
|
||||
}
|
||||
|
||||
int Sprite::GetShiftArrayX(int pos) {
|
||||
return GetShiftArray(pos, x_shift, width);
|
||||
}
|
||||
|
@ -1,9 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef CASCADE_GRAPHICS_SPRITE_H_
|
||||
#define CASCADE_GRAPHICS_SPRITE_H_
|
||||
#ifndef DROPLETS_GRAPHICS_SPRITE_H_
|
||||
#define DROPLETS_GRAPHICS_SPRITE_H_
|
||||
|
||||
#include "daisy_patch.h"
|
||||
#include "../util.h"
|
||||
|
||||
using namespace daisy;
|
||||
|
||||
@ -13,19 +14,7 @@ class Sprite {
|
||||
bool** sprite;
|
||||
int x_shift = 0;
|
||||
int y_shift = 0;
|
||||
|
||||
/*
|
||||
* Converts a graphic pixel location to a new one based on
|
||||
* a given shift.
|
||||
*
|
||||
* @param pos pixel position
|
||||
* @param shift shift amount
|
||||
* @param array_size max length of dimension
|
||||
* return new pixel position
|
||||
*/
|
||||
int GetShiftArray(int pos,
|
||||
int shift,
|
||||
int array_size);
|
||||
|
||||
/*
|
||||
* Converts a x axis position based upon the sprites shift.
|
||||
*
|
||||
@ -163,4 +152,4 @@ class Sprite {
|
||||
void AdjustYShift(int y);
|
||||
};
|
||||
|
||||
#endif // CASCADE_GRAPHICS_SPRITE_H_
|
||||
#endif // DROPLETS_GRAPHICS_SPRITE_H_
|
||||
|
Reference in New Issue
Block a user