Browse Source

Added pixels to graphics (sprite with changeable pixels)

master
Macoy Madson 6 years ago
parent
commit
a0e47e558c
7 changed files with 131 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +88
    -0
      graphics/graphics.cpp
  3. +39
    -0
      graphics/graphics.hpp
  4. +2
    -0
      makefile
  5. BIN
      tools/multilayerMapEditor
  6. BIN
      tools/tileEditor
  7. BIN
      tools/tiledImporter

+ 2
- 0
.gitignore View File

@ -26,3 +26,5 @@
*.exe
*.out
*.app
LOCAL*

+ 88
- 0
graphics/graphics.cpp View File

@ -28,6 +28,10 @@ void window::draw(text *txt)
{
if (txt) win->draw(txt->str);
}
void window::draw(pixels *pix)
{
if (pix) win->draw(pix->spr.spr);
}
void window::update()
{
time.start();
@ -192,4 +196,88 @@ sf::Sprite* sprite::getBase()
{
return &spr;
}
pixels::pixels(int newWidth, int newHeight)
{
width = newWidth;
height = newHeight;
rawPixels = new sf::Uint8[width * height * 4];
//Blank the pixels
for (int i = 0; i < height; ++i)
{
for (int n = 0; n < width; ++n)
{
int startIndex = (i * width * 4) + (n * 4);
rawPixels[startIndex] = 0;
rawPixels[startIndex + 1] = 0;
rawPixels[startIndex + 2] = 0;
rawPixels[startIndex + 3] = 0;
}
}
texture.create(width, height);
texture.update(rawPixels);
spr.getBase()->setTexture(texture);
}
pixels::pixels(int newWidth, int newHeight, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
width = newWidth;
height = newHeight;
rawPixels = new sf::Uint8[width * height * 4];
//Blank the pixels
for (int i = 0; i < height; ++i)
{
for (int n = 0; n < width; ++n)
{
int startIndex = (i * width * 4) + (n * 4);
rawPixels[startIndex] = r;
rawPixels[startIndex + 1] = g;
rawPixels[startIndex + 2] = b;
rawPixels[startIndex + 3] = a;
}
}
texture.create(width, height);
texture.update(rawPixels);
spr.getBase()->setTexture(texture);
spr.setSmooth(false);
}
sprite* pixels::getSprite()
{
return &spr;
}
void pixels::setPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
if (x >= 0 && x < width && y >= 0 && y < height)
{
int startIndex = (y * width * 4) + (x * 4);
rawPixels[startIndex] = r;
rawPixels[startIndex + 1] = g;
rawPixels[startIndex + 2] = b;
rawPixels[startIndex + 3] = a;
}
}
void pixels::update()
{
texture.update(rawPixels);
}
bool pixels::getPixel(int x, int y, unsigned char& r, unsigned char& g, unsigned char& b, unsigned char& a)
{
if (x >= 0 && x < width && y >= 0 && y < height)
{
int startIndex = (y * width * 4) + (x * 4);
r = rawPixels[startIndex];
g = rawPixels[startIndex + 1];
b = rawPixels[startIndex + 2];
a = rawPixels[startIndex + 3];
return true;
}
else return false;
}
int pixels::getWidth()
{
return width;
}
int pixels::getHeight()
{
return height;
}
#endif

+ 39
- 0
graphics/graphics.hpp View File

@ -66,6 +66,44 @@ public:
//Return a direct pointer to the base sprite (lib dependant)
sf::Sprite* getBase();
};
//An image with individually changeable pixels
class pixels
{
friend class window;
private:
int width;
int height;
sf::Uint8* rawPixels;
sf::Texture texture;
sprite spr;
public:
//Creates the pixel array and sets the pixels to (0, 0, 0, 0);
//sets up the primary sprite as well. Note that the primary sprite
//smoothing is set to off by default
pixels(int newWidth, int newHeight);
//Creates the pixel array and sets the pixels to the provided color
//sets up the primary sprite as well. Note that the primary sprite
//smoothing is set to off by default
pixels(int newWidth, int newHeight, unsigned char r, unsigned char g, unsigned char b, unsigned char a);
//Use this function to manipulate the position etc. of the pixels
//through the master sprite
sprite* getSprite();
//Manipulate the color of a single pixel (RGBA); does bounds checking.
//Remember to call update() in order to see changes
void setPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a);
//Call after changing any pixels in order to see changes
void update();
//Sets the provided values to the color of the specified pixel; returns
//false if out of bounds
bool getPixel(int x, int y, unsigned char& r, unsigned char& g, unsigned char& b, unsigned char& a);
//Returns the width and height of the image
int getWidth();
int getHeight();
};
//The core of the graphics, the window.
class window
{
@ -82,6 +120,7 @@ class window
//Various drawing functions
void draw(sprite * spr);
void draw(text *txt);
void draw(pixels *pix);
//Update the window
void update();
//Test if the window should close


+ 2
- 0
makefile View File

@ -1,9 +1,11 @@
FLAGS=g++ -c -Os -Wall
ARCHIVE_NAME=lib/base.a
OBJ_DIR=LOCAL_obj
all: eptParser.o math.o collision.o timer.o profiler.o graphics.o input.o tileMap.o multilayerMap.o tileCamera.o animation.o sound.o
@echo -------------------Building archive...
ar rvs $(ARCHIVE_NAME) eptParser.o math.o collision.o graphics.o input.o tileMap.o multilayerMap.o tileCamera.o animation.o timer.o profiler.o sound.o
#mv *.o $(OBJ_DIR)
eptParser.o: ept/eptParser.hpp ept/eptParser.cpp
$(FLAGS) "ept/eptParser.hpp"
$(FLAGS) "ept/eptParser.cpp"


BIN
tools/multilayerMapEditor View File


BIN
tools/tileEditor View File


BIN
tools/tiledImporter View File


Loading…
Cancel
Save