Skip to content

Commit

Permalink
More drawing functions out of editor and into canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
kieranmillar committed Nov 20, 2017
1 parent b32e3f3 commit 7bf4af4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 59 deletions.
59 changes: 54 additions & 5 deletions src/Editor/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,27 @@ void Canvas::draw()
{
Level::Object &o = level_ptr->object[i->type][i->i];
int so = style_ptr->object_by_id(i->type, o.id);
editor_ptr->draw_selection_box((o.x - scroll_x)*zoom, (o.y - scroll_y)*zoom, style_ptr->object[i->type][so].width * 8 * zoom, style_ptr->object[i->type][so].height * 2 * zoom);
draw_selection_box((o.x - scroll_x)*zoom, (o.y - scroll_y)*zoom, style_ptr->object[i->type][so].width * 8 * zoom, style_ptr->object[i->type][so].height * 2 * zoom);
}

SDL_SetRenderDrawColor(window_ptr->screen_renderer, 0, 0, 0, 255);

//Draw dotted lines on the level border
if (scroll_x < 0 && (scroll_x * zoom) + window_ptr->width > 0)
{
editor_ptr->draw_dashed_level_border(Editor::vertical, ((0 - scroll_x) * zoom) - 1, scroll_y * zoom);
draw_dashed_level_border(vertical, ((0 - scroll_x) * zoom) - 1, scroll_y * zoom);
}
if (scroll_x < level_ptr->width && scroll_x + (window_ptr->width / zoom) > level_ptr->width)
{
editor_ptr->draw_dashed_level_border(Editor::vertical, ((level_ptr->width - scroll_x) * zoom), scroll_y * zoom);
draw_dashed_level_border(vertical, ((level_ptr->width - scroll_x) * zoom), scroll_y * zoom);
}
if (scroll_y < 0 && (scroll_y * zoom) + window_ptr->height > 0)
{
editor_ptr->draw_dashed_level_border(Editor::horizontal, ((0 - scroll_y) * zoom) - 1, scroll_x * zoom);
draw_dashed_level_border(horizontal, ((0 - scroll_y) * zoom) - 1, scroll_x * zoom);
}
if (scroll_y < level_ptr->height && scroll_y + (window_ptr->height / zoom) > level_ptr->height)
{
editor_ptr->draw_dashed_level_border(Editor::horizontal, ((level_ptr->height - scroll_y) * zoom), scroll_x * zoom);
draw_dashed_level_border(horizontal, ((level_ptr->height - scroll_y) * zoom), scroll_x * zoom);
}

SDL_SetRenderTarget(window_ptr->screen_renderer, NULL);
Expand All @@ -117,4 +117,53 @@ void Canvas::draw()
SDL_RenderPresent(window_ptr->screen_renderer);


}

void Canvas::draw_selection_box(int x, int y, int width, int height)
{
if (x > window_ptr->width || y > (window_ptr->height - BAR_HEIGHT) || (x + width) < 0 || (x + height) < 0)
return;
SDL_SetRenderDrawColor(window_ptr->screen_renderer, 255, 0, 255, 255 / 8);
SDL_Rect r;
r.x = x - 1;
r.y = y - 1;
r.w = width + 1;
r.h = height + 1;
SDL_RenderFillRect(window_ptr->screen_renderer, &r);
SDL_SetRenderDrawColor(window_ptr->screen_renderer, 255, 0, 255, 255);
SDL_RenderDrawRect(window_ptr->screen_renderer, &r);
}

void Canvas::draw_dashed_level_border(borderType type, int pos, int offset)
{
//We have an offset so the lines don't scroll out of synch with the view when scrolling
int initialOffset = offset % 20;
SDL_SetRenderDrawColor(window_ptr->screen_renderer, 200, 200, 200, 255);
int end, x1, y1, x2, y2;
if (type == horizontal)
{
end = window_ptr->width;
x1 = 0 - initialOffset;
x2 = 10 - initialOffset;
y1 = pos;
y2 = pos;
for (x1; x1 < end; x1 += 20)
{
SDL_RenderDrawLine(window_ptr->screen_renderer, x1, y1, x2, y2);
x2 += 20;
}
}
if (type == vertical)
{
end = window_ptr->height;
x1 = pos;
x2 = pos;
y1 = 0 - initialOffset;
y2 = 10 - initialOffset;
for (y1; y1 < end; y1 += 20)
{
SDL_RenderDrawLine(window_ptr->screen_renderer, x1, y1, x2, y2);
y2 += 20;
}
}
}
5 changes: 5 additions & 0 deletions src/Editor/canvas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class Canvas

void draw(void);

void draw_selection_box(int x, int y, int width, int height);

enum borderType { horizontal, vertical };
void draw_dashed_level_border(borderType type, int pos, int offset);

Canvas(void) { /* nothing to do */ }

};
Expand Down
49 changes: 0 additions & 49 deletions src/Editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,53 +236,4 @@ bool Editor::scroll( signed int delta_x, signed int delta_y, bool drag )
}

return false;
}

void Editor::draw_selection_box(int x, int y, int width, int height)
{
if (x > window_ptr->width || y > (window_ptr->height - BAR_HEIGHT) || (x + width) < 0 || (x + height) < 0)
return;
SDL_SetRenderDrawColor(window_ptr->screen_renderer, 255, 0, 255, 255 / 8);
SDL_Rect r;
r.x = x - 1;
r.y = y - 1;
r.w = width + 1;
r.h = height + 1;
SDL_RenderFillRect(window_ptr->screen_renderer, &r);
SDL_SetRenderDrawColor(window_ptr->screen_renderer, 255, 0, 255, 255);
SDL_RenderDrawRect(window_ptr->screen_renderer, &r);
}

void Editor::draw_dashed_level_border(borderType type, int pos, int offset)
{
//We have an offset so the lines don't scroll out of synch with the view when scrolling
int initialOffset = offset % 20;
SDL_SetRenderDrawColor(window_ptr->screen_renderer, 200, 200, 200, 255);
int end, x1, y1, x2, y2;
if (type == horizontal)
{
end = window_ptr->width;
x1 = 0 - initialOffset;
x2 = 10 - initialOffset;
y1 = pos;
y2 = pos;
for (x1; x1 < end; x1 += 20)
{
SDL_RenderDrawLine(window_ptr->screen_renderer, x1, y1, x2, y2);
x2 += 20;
}
}
if (type == vertical)
{
end = window_ptr->height;
x1 = pos;
x2 = pos;
y1 = 0 - initialOffset;
y2 = 10 - initialOffset;
for (y1; y1 < end; y1 += 20)
{
SDL_RenderDrawLine(window_ptr->screen_renderer, x1, y1, x2, y2);
y2 += 20;
}
}
}
5 changes: 0 additions & 5 deletions src/Editor/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ class Editor

bool load(int n, Window * w);
bool save(int n);

void draw_selection_box(int x, int y, int width, int height);

enum borderType { horizontal, vertical };
void draw_dashed_level_border(borderType type, int pos, int offset);

Editor( void );
~Editor( void ) { /* nothing to do */ }
Expand Down

0 comments on commit 7bf4af4

Please sign in to comment.