Improve SLK_draw_rgb(.c)

Improved code style
Added comments
This commit is contained in:
Captain4LK 2020-04-28 21:43:13 +02:00
parent 3e8e019bc1
commit 5f2ae526e3

View file

@ -13,14 +13,44 @@ Redistribution and use in source and binary forms, with or without modification,
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "../../include/SLK/SLK.h"
#include "SLK_variables.h"
//External includes
//-------------------------------------
//Internal includes
#include "../../include/SLK/SLK_types.h"
#include "../../include/SLK/SLK_functions.h"
#include "SLK_variables.h"
//-------------------------------------
//#defines
#define INBOUNDS(LOWER,UPPER,NUMBER) \
((unsigned)(NUMBER-LOWER)<=(UPPER-LOWER))
#define SIGNUM(NUM) \
NUM==0?0:(NUM<0?-1:1)
#define SWAP(x,y) \
{ (x)=(x)^(y); (y)=(x)^(y); (x)=(x)^(y); }
//-------------------------------------
//Typedefs
//-------------------------------------
//Variables
//-------------------------------------
//Function prototypes
//-------------------------------------
//Function implementations
//Gets the current draw target.
//Usefull for getting the default draw target.
SLK_RGB_sprite *SLK_draw_rgb_get_target()
{
return target_rgb;
}
//Sets the draw target for rgb drawing operations.
//Pass NULL to set to default draw target.
void SLK_draw_rgb_set_target(SLK_RGB_sprite *s)
{
if(s==NULL)
@ -32,23 +62,30 @@ void SLK_draw_rgb_set_target(SLK_RGB_sprite *s)
target_rgb = s;
}
//Sets the color the draw target is to be cleared to
//when calling SLK_draw_rgb_clear.
void SLK_draw_rgb_set_clear_color(SLK_Color color)
{
target_rgb_clear = color;
}
//Clears the draw target to the color specified
//by SKL_draw_rgb_set_clear_color.
void SLK_draw_rgb_clear()
{
for(int i = 0;i<target_rgb->width*target_rgb->height;i++)
target_rgb->data[i] = target_rgb_clear;
}
//Draws a single pixel to the draw target.
void SLK_draw_rgb_color(int x, int y, SLK_Color color)
{
if(color.a&&INBOUNDS(0,target_rgb->width,x)&&INBOUNDS(0,target_rgb->height,y))
target_rgb->data[y*target_rgb->width+x] = color;
}
//Draws a string to the draw target.
//Color and scale must be specified.
void SLK_draw_rgb_string(int x, int y, int scale, const char *text, SLK_Color color)
{
int sx = 0;
@ -87,6 +124,11 @@ void SLK_draw_rgb_string(int x, int y, int scale, const char *text, SLK_Color co
}
}
//Draws a sprite to the draw target.
//SoftLK does NOT do alpha blending in the software
//accelerated layers, but if you draw alpha,
//alpha blending will be applied to the layers below
//the current one.
void SLK_draw_rgb_sprite(const SLK_RGB_sprite *s, int x, int y)
{
int draw_start_y = 0;
@ -114,6 +156,12 @@ void SLK_draw_rgb_sprite(const SLK_RGB_sprite *s, int x, int y)
}
}
//Draws a specified part of a sprite, can be
//used for sprite sheets.
//There is no performance benefitt in doing
//so since rgb drawing is software accelerated
//and the sprite data is located in RAM rather
//than in VRAM.
void SLK_draw_rgb_sprite_partial(const SLK_RGB_sprite *s, int x, int y, int ox, int oy, int width, int height)
{
for(int tx = 0; tx < width; tx++)
@ -126,6 +174,9 @@ void SLK_draw_rgb_sprite_partial(const SLK_RGB_sprite *s, int x, int y, int ox,
}
}
//Draws a flipped sprite.
//Pass SLK_FLIP_VERTICAL for vertical flipping and
//SLK_FLIP_HORIZONTAL for horizontal flipping.
void SLK_draw_rgb_sprite_flip(const SLK_RGB_sprite *s, int x, int y, int flip)
{
int draw_start_y = 0;
@ -192,6 +243,8 @@ void SLK_draw_rgb_sprite_flip(const SLK_RGB_sprite *s, int x, int y, int flip)
}
}
//Draws a line between to points
//using the Bresenham line drawing algorythm.
void SLK_draw_rgb_line(int x0, int y0, int x1, int y1, SLK_Color color)
{
int changed = 0;
@ -235,18 +288,24 @@ void SLK_draw_rgb_line(int x0, int y0, int x1, int y1, SLK_Color color)
}
//Draws a line between two points
//with fixed x coordinates.
void SLK_draw_rgb_vertical_line(int x, int y0, int y1, SLK_Color color)
{
for(int y = y0;y<y1;y++)
SLK_draw_rgb_color(x,y,color);
}
//Draws a line between two points
//with fixed y coordinates
void SLK_draw_rgb_horizontal_line(int x0, int x1, int y, SLK_Color color)
{
for(int x = x0;x<x1;x++)
SLK_draw_rgb_color(x,y,color);
}
//Draws the outline of a colored
//rectangle.
void SLK_draw_rgb_rectangle(int x, int y, int width, int height, SLK_Color color)
{
for(int i = x;i<x+width;i++)
@ -262,6 +321,7 @@ void SLK_draw_rgb_rectangle(int x, int y, int width, int height, SLK_Color color
}
}
//Draws a solid colored rectangle.
void SLK_draw_rgb_fill_rectangle(int x, int y, int width, int height, SLK_Color color)
{
for(int x_ = x;x_<x+width;x_++)
@ -269,6 +329,7 @@ void SLK_draw_rgb_fill_rectangle(int x, int y, int width, int height, SLK_Color
SLK_draw_rgb_color(x_,y_,color);
}
//Draws the outline of a colored circle.
void SLK_draw_rgb_circle(int x, int y, int radius, SLK_Color color)
{
int x_ = 0;
@ -306,6 +367,7 @@ void SLK_draw_rgb_circle(int x, int y, int radius, SLK_Color color)
}
}
//Draws a solid colored circle.
void SLK_draw_rgb_fill_circle(int x, int y, int radius, SLK_Color color)
{
int x_ = 0;
@ -334,3 +396,4 @@ void SLK_draw_rgb_fill_circle(int x, int y, int radius, SLK_Color color)
SLK_draw_rgb_horizontal_line(x-y_,x+y_,y-x_,color);
}
}
//-------------------------------------