Verified Commit ab39bda1 authored by RobLoach's avatar RobLoach
Browse files

Add Rotation and Scaling for love.graphics.draw()

parent f1f8646d
......@@ -6,11 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased] - xxxx-xx-xx
### Added
- Introduced `love` namespace
- You can now call `love.graphics.draw()`
- `love` namespace
- Example: `love.graphics.draw()`
- Rotation and scaling for `love.graphics.draw()`
### Changed
- ChaiScript_Extras is now using its original source, as the upstream GCC issue is fixed
- ChaiScript_Extras now using original source, upstream GCC issue fixed
- Updated libretro-common
- Updated random
- Updated sdl-libretro
......
......@@ -3,6 +3,7 @@
#include <SDL.h>
#include <SDL_gfxPrimitives.h>
#include <SDL_gfxBlitFunc.h>
#include <SDL_rotozoom.h>
#include "../ChaiLove.h"
#include "../Types/Graphics/Image.h"
......@@ -81,21 +82,60 @@ void graphics::line(int x1, int y1, int x2, int y2) {
void graphics::draw(Image* image, int x, int y) {
if (image && image->loaded()) {
SDL_Rect* dstrect = new SDL_Rect();
dstrect->x = x;
dstrect->y = y;
SDL_BlitSurface(image->surface, NULL, getScreen(), dstrect);
SDL_Rect dstrect;
dstrect.x = x;
dstrect.y = y;
SDL_BlitSurface(image->surface, NULL, getScreen(), &dstrect);
}
}
void graphics::draw(Image* image, Quad quad, int x, int y) {
if (image && image->loaded()) {
SDL_Rect* dest = new SDL_Rect();
dest->x = x;
dest->y = y;
dest->w = x + quad.width;
dest->h = y + quad.height;
SDL_BlitSurface(image->surface, quad.toRect(), getScreen(), dest);
SDL_Rect dest;
dest.x = x;
dest.y = y;
dest.w = x + quad.width;
dest.h = y + quad.height;
SDL_Rect src = quad.toRect();
SDL_BlitSurface(image->surface, &src, getScreen(), &dest);
}
}
void graphics::draw(Image* image, int x, int y, float r, float sx, float sy, float ox, float oy) {
if (image && image->loaded()) {
ChaiLove* app = ChaiLove::getInstance();
int smooth = app->config.options["alphablending"];
float angle = app->math.degrees(r);
SDL_Surface* tempSurface = rotozoomSurfaceXY(image->surface, angle, sx, sy, smooth);
if (tempSurface) {
float aspectX = ox / image->getWidth();
float aspectY = oy / image->getHeight();
SDL_Rect dstrect;
dstrect.x = x - aspectX * tempSurface->w;
dstrect.y = y - aspectY * tempSurface->h;
SDL_BlitSurface(tempSurface, NULL, getScreen(), &dstrect);
SDL_FreeSurface(tempSurface);
}
}
}
void graphics::draw(Image* image, int x, int y, float r, float sx, float sy, float ox) {
draw(image, x, y, r, sx, sy, ox, 0.0f);
}
void graphics::draw(Image* image, int x, int y, float r, float sx, float sy) {
draw(image, x, y, r, sx, sy, 0.0f, 0.0f);
}
void graphics::draw(Image* image, int x, int y, float r, float sx) {
draw(image, x, y, r, sx, sx, 0.0f, 0.0f);
}
void graphics::draw(Image* image, int x, int y, float r) {
if (r == 0.0f) {
draw(image, x, y);
} else {
draw(image, x, y, r, 1.0f, 1.0f, 0.0f, 0.0f);
}
}
......
......@@ -198,6 +198,24 @@ class graphics {
*/
void ellipse(const std::string& drawmode, int x, int y, int radiusx, int radiusy);
/**
* @brief Draws an image with the given angle, zoom, and origin.
*
* @param image The image to draw on the screen.
* @param x (0) The position to draw the object (x-axis).
* @param y (0) The position to draw the object (y-axis).
* @param r (0) Orientation (radians).
* @param sx (1) Scale factor (x-axis).
* @param sy (sx) Scale factor (y-axis).
* @param ox (0) Origin offset (x-axis).
* @param oy (0) Origin offset (y-axis).
*/
void draw(Image* image, int x, int y, float r, float sx, float sy, float ox, float oy);
void draw(Image* image, int x, int y, float r, float sx, float sy, float ox);
void draw(Image* image, int x, int y, float r, float sx, float sy);
void draw(Image* image, int x, int y, float r, float sx);
void draw(Image* image, int x, int y, float r);
/**
* @brief Draws an arc.
*/
......
......@@ -123,4 +123,13 @@ std::string math::decompress(const std::string& str) {
return outstring;
}
float math::rad(float degress) {
return degress * pi / 180.0f;
}
float math::degrees(float rad) {
return rad * 180.0f / pi;
}
} // namespace Modules
......@@ -37,6 +37,16 @@ class math {
*/
int getRandomSeed();
/**
* @brief Converts from degrees to radians.
*/
float rad(float degrees);
/**
* @brief Converts from radians to degrees.
*/
float degrees(float rad);
std::string compress(const std::string& str);
/**
......
......@@ -178,7 +178,14 @@ script::script(const std::string& file) {
chai.add(fun<void, graphics, int, int, int>(&graphics::setColor), "setColor");
chai.add(fun<void, graphics, int, int, int, int>(&graphics::setBackgroundColor), "setBackgroundColor");
chai.add(fun<void, graphics, int, int, int>(&graphics::setBackgroundColor), "setBackgroundColor");
chai.add(fun<void, graphics, Image*, int, int>(&graphics::draw), "draw");
chai.add(fun<void, graphics, Image*, int, int, float, float, float, float, float>(&graphics::draw), "draw");
chai.add(fun<void, graphics, Image*, int, int, float, float, float, float>(&graphics::draw), "draw");
chai.add(fun<void, graphics, Image*, int, int, float, float, float>(&graphics::draw), "draw");
chai.add(fun<void, graphics, Image*, int, int, float, float>(&graphics::draw), "draw");
chai.add(fun<void, graphics, Image*, int, int, float>(&graphics::draw), "draw");
chai.add(fun<void, graphics, Image*, Quad, int, int>(&graphics::draw), "draw");
chai.add(fun<void, graphics, int, int, int, int>(&graphics::clear), "clear");
chai.add(fun<void, graphics, int, int, int>(&graphics::clear), "clear");
......@@ -271,6 +278,8 @@ script::script(const std::string& file) {
chai.add(mathlib);
chai.add(fun(&math::pi), "pi");
chai.add(fun(&math::e), "e");
chai.add(fun(&math::rad), "rad");
chai.add(fun(&math::degrees), "degrees");
chai.add(fun<float, math>(&math::random), "random");
chai.add(fun<float, math, float>(&math::random), "random");
chai.add(fun<float, math, float, float>(&math::random), "random");
......
......@@ -42,8 +42,9 @@ bool Image::loadFromRW(SDL_RWops* rw) {
SDL_SetColorKey(surface, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
optimizedImage = SDL_DisplayFormat(surface);
}
if (!optimizedImage) {
std::cout << "SDL_DisplayFormat failed to optimize the image." << std::endl;
std::cout << "[ChaiLove] [graphics] SDL_DisplayFormat failed to optimize the image." << std::endl;
} else {
SDL_FreeSurface(surface);
surface = optimizedImage;
......
......@@ -17,12 +17,12 @@ Quad::Quad() {
// Nothing.
}
SDL_Rect* Quad::toRect() {
SDL_Rect* rect = new SDL_Rect();
rect->x = x;
rect->y = y;
rect->w = width;
rect->h = height;
SDL_Rect Quad::toRect() {
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = width;
rect.h = height;
return rect;
}
......
......@@ -15,7 +15,7 @@ class Quad {
Quad();
Quad(int x, int y, int width, int height, int sw, int sh);
Quad(int x, int y, int width, int height);
SDL_Rect* toRect();
SDL_Rect toRect();
};
} // namespace Graphics
......
global keyPressed = "a";
global keyReleased = "b";
global logo
global angle = 0.0f
global scale = 1.0f
def load() {
logo = love.image.newImageData("assets/graphics_draw.png")
graphics.setBackgroundColor(50, 50, 100)
}
def update(dt) {
angle = angle - dt * 180.0f
if (keyPressed == "up") {
scale = scale + dt * 1.0f
}
else if (keyPressed == "down") {
scale = scale - dt * 1.0f
}
}
def draw() {
graphics.print("Key Pressed: " + keyPressed, 5, 20)
graphics.print("Key Released: " + keyReleased, 5, 100)
graphics.setColor(255, 0, 0)
//love.graphics.draw(logo, graphics.getWidth() / 2, graphics.getHeight() / 2, math.rad(angle), 2, 2, 64/2, 64/2)
love.graphics.point(graphics.getWidth() / 2.0f, graphics.getHeight() / 2.0f)
love.graphics.draw(logo, graphics.getWidth() / 2.0f, graphics.getHeight() / 2.0f, math.rad(angle), scale, scale, logo.getWidth() / 2.0f, logo.getHeight() / 2.0f)
love.graphics.point(graphics.getWidth() / 2.0f, graphics.getHeight() / 2.0f)
//love.graphics.point(graphics.getWidth() / 2
}
def keypressed(key, scancode) {
......
assert_equal(love.graphics.getWidth(), 800, "love.graphics.getWidth()")
assert_equal(love.graphics.getHeight(), 600, "love.graphics.getHeight()")
......@@ -8,6 +8,7 @@ def load() {
filesystem.load("audio.chai")
filesystem.load("filesystem.chai")
filesystem.load("font.chai")
filesystem.load("graphics.chai")
filesystem.load("math.chai")
filesystem.load("keyboard.chai")
filesystem.load("timer.chai")
......
......@@ -21,6 +21,5 @@ assert_not_equal(text, compressed, "math.compress()")
var decompressed = math.decompress(compressed)
assert_equal(decompressed, text, "math.decompress()")
assert(love.math.pi > 3.0f, "math.pi")
assert(love.math.pi < 4.0f, "math.pi")
assert(love.math.pi > 3.0f && love.math.pi < 4.0f, "math.pi")
assert(love.math.e > 2.0f, "math.e")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment