Skip to content

Commit

Permalink
Added some miscellaneous things
Browse files Browse the repository at this point in the history
  • Loading branch information
gorbit99 committed Oct 28, 2019
1 parent 47c90a1 commit 168a4c8
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 24 deletions.
5 changes: 5 additions & 0 deletions Camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ void camera_zoom(Camera *camera, float zoomLevels, Point zoomPos) {
void camera_move(Camera *camera, Vec movement) {
camera->position.x -= movement.x / camera->zoom;
camera->position.y -= movement.y / camera->zoom;
}

Point camera_screen_to_view(Camera *camera, Point p) {
return (Point){(p.x) / camera->zoom + camera->position.x,
(p.y) / camera->zoom + camera->position.y};
}
2 changes: 2 additions & 0 deletions Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ typedef struct Camera {
void camera_zoom(Camera *camera, float zoomLevels, Point zoomPos);
void camera_move(Camera *camera, Vec movement);

Point camera_screen_to_view(Camera *camera, Point p);

#endif //HOMEWORK_CAMERA_H
118 changes: 109 additions & 9 deletions GUIGraphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,126 @@ NSliceTexture guigfx_create_nslice(char *path, SliceType type, int x1, int y1, i
texture,
w, h,
type,
x1, y1, x2, y2
x1, y1, x2, y2,
x1, x2 - x1, w - x2,
y1, y2 - y1, h - y2
};
return result;
}

void guigfx_render_9slice(NSliceTexture *data, SDL_Rect rect, SDL_Renderer *renderer) {
SDL_Rect src, dst;

int x1, x2, x3;
int y1, y2, y3;
int w1, w2, w3;
int h1, h2, h3;

x1 = rect.x; x2 = rect.x + data->w1; x3 = rect.x + rect.w - data->w3;
y1 = rect.y; y2 = rect.y + data->h1; y3 = rect.y + rect.h - data->h3;
w1 = x2 - x1; w2 = x3 - x2; w3 = x1 + rect.w - x3;
h1 = y2 - y1; h2 = y3 - y2; h3 = y1 + rect.h - y3;

//Corners
src = (SDL_Rect){0, 0, data->x1, data->y1};
dst = (SDL_Rect){rect.x + src.x, rect.y + src.y, src.w, src.h};
src = (SDL_Rect){0, 0, data->w1, data->h1};
dst = (SDL_Rect){x1, y1, w1, h1};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
src = (SDL_Rect){data->x2, 0, data->w - data->x2, data->y1};
dst = (SDL_Rect){rect.x + rect.w - src.w, rect.y + src.y, src.w, src.h};
src = (SDL_Rect){data->x2, 0, data->w3, data->h1};
dst = (SDL_Rect){x3, y1, w3, h1};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
src = (SDL_Rect){0, data->y2, data->x1, data->h - data->y2};
dst = (SDL_Rect){rect.x + src.x, rect.y + rect.h - src.h, src.w, src.h};
src = (SDL_Rect){0, data->y2, data->w1, data->h3};
dst = (SDL_Rect){x1, y3, w1, h3};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
src = (SDL_Rect){data->x2, data->y2, data->w - data->x2, data->h - data->y2};
dst = (SDL_Rect){rect.x + rect.w - src.w, rect.y + rect.h - src.h, src.w, src.h};
src = (SDL_Rect){data->x2, data->y2, data->w3, data->h3};
dst = (SDL_Rect){x3, y3, w3, h3};
SDL_RenderCopy(renderer, data->texture, &src, &dst);

switch (data->type)
{
case ST_CLAMP: {
//Top
src = (SDL_Rect){data->x1, 0, data->w2, data->h1};
dst = (SDL_Rect){x2, y1, w2, h1};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
//Bottom
src = (SDL_Rect){data->x1, data->y2, data->w2, data->h3};
dst = (SDL_Rect){x2, y3, w2, h3};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
//Left
src = (SDL_Rect){0, data->y1, data->w1, data->h2};
dst = (SDL_Rect){x1, y2, w1, h2};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
//Right
src = (SDL_Rect){data->x2, data->y1, data->w3, data->h2};
dst = (SDL_Rect){x3, y2, w3, h2};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
//Center
src = (SDL_Rect){data->x1, data->y1, data->w2, data->h2};
dst = (SDL_Rect){x2, y2, w2, h2};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
break;
}
case ST_REPEAT: {
//Top
src = (SDL_Rect){data->x1, 0, data->w2, data->h1};
int x;
for (x = x2; x < x3 - data->w2; x += data->w2) {
dst = (SDL_Rect){x, y1, data->w2, h1};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
}
src.w = x3 - x;
dst = (SDL_Rect){x, y1, x3 - x, h1};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
//Bottom
src = (SDL_Rect){data->x1, data->x2, data->w2, data->h3};
for (x = x2; x < x3 - data->w2; x += data->w2) {
dst = (SDL_Rect){x, y3, data->w2, h3};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
}
src.w = x3 - x;
dst = (SDL_Rect){x, y3, x3 - x, h3};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
//Left
src = (SDL_Rect){0, data->y1, data->w1, data->h2};
int y;
for (y = y2; y < y3 - data->h2; y += data->h2) {
dst = (SDL_Rect){x1, y, w1, data->h2};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
}
src.h = y3 - y;
dst = (SDL_Rect){x1, y, w1, y3 - y};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
//Right
src = (SDL_Rect){data->x2, data->y1, data->w3, data->h2};
for (y = y2; y < y3 - data->h2; y += data->h2) {
dst = (SDL_Rect){x3, y, w3, data->h2};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
}
src.h = y3 - y;
dst = (SDL_Rect){x3, y, w3, y3 - y};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
//Center
for (x = x2; x < x3 - data->w2; x += data->w2) {
src = (SDL_Rect){data->x1, data->y1, data->w2, data->h2};
for (y = y2; y < y3 - data->h2; y += data->h2) {
dst = (SDL_Rect){x, y, data->w2, data->h2};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
}
src.h = y3 - y;
dst = (SDL_Rect){x, y, data->w2, y3 - y};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
}
src = (SDL_Rect){data->x1, data->y1, x3 - x, data->h2};
for (y = y2; y < y3 - data->h2; y += data->h2) {
dst = (SDL_Rect){x, y, x3 - x, data->h2};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
}
src.h = y3 - y;
dst = (SDL_Rect){x, y, x3 - x, y3 - y};
SDL_RenderCopy(renderer, data->texture, &src, &dst);
break;
}
default:
break;
}
}
7 changes: 5 additions & 2 deletions GUIGraphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@

#include <SDL.h>
#include <SDL_image.h>
#include <stdbool.h>
#include "Utility.h"

typedef enum SliceType {
ST_CLAMP,
ST_REPEAT,
ST_MIRROR
ST_REPEAT
} SliceType;

typedef struct NSliceTexture {
SDL_Texture *texture;
int w, h;
SliceType type;
int x1, y1, x2, y2;
int w1, w2, w3;
int h1, h2, h3;
} NSliceTexture;

NSliceTexture guigfx_create_nslice(char *path, SliceType type, int x1, int y1, int x2, int y2, SDL_Renderer *renderer);
Expand Down
13 changes: 9 additions & 4 deletions Input.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ void input_handle_event(SDL_Event *e) {
}
break;
case SDL_MOUSEMOTION:
mouseDeltaX = e->motion.xrel;
mouseDeltaY = e->motion.yrel;
mouseDeltaX += e->motion.xrel;
mouseDeltaY += e->motion.yrel;
mouseX = e->motion.x;
mouseY = e->motion.y;
break;
case SDL_MOUSEWHEEL:
mouseWheelX = e->wheel.x;
mouseWheelY = e->wheel.y;
mouseWheelX += e->wheel.x;
mouseWheelY += e->wheel.y;
if (e->wheel.direction == SDL_MOUSEWHEEL_FLIPPED) {
mouseWheelX *= -1;
mouseWheelY *= -1;
Expand Down Expand Up @@ -132,4 +132,9 @@ Vec input_get_mouse_wheel() {
(float)input_get_mouse_wheel_x(),
(float)input_get_mouse_wheel_y()
};
}

bool input_mouse_over(SDL_Rect r) {
SDL_Point p = {input_get_mouse_x(), input_get_mouse_y()};
return SDL_PointInRect(&p, &r) == SDL_TRUE;
}
2 changes: 2 additions & 0 deletions Input.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ int input_get_mouse_wheel_x();
int input_get_mouse_wheel_y();
Vec input_get_mouse_wheel();

bool input_mouse_over(SDL_Rect r);

#endif //HOMEWORK_INPUT_H
8 changes: 8 additions & 0 deletions Node.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,11 @@ void node_render(Node *node, Point camPos) {
component_render(&node->component, node->renderer, camPos, 0x0000aaff);
}
}

bool node_is_over(Node *node, Point p) {
if (!input_get_mouse_button(SDL_BUTTON_LEFT).isPressed)
return false;
SDL_Point sp = {(int)p.x, (int)p.y};
SDL_Rect rect = {node->component.x - node->component.w / 2,node->component.y - node->component.h / 2, node->component.w, node->component.h};
return SDL_PointInRect(&sp, &rect);
}
3 changes: 3 additions & 0 deletions Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define HOMEWORK_NODE_H

#include "Component.h"
#include "Input.h"

struct Node;

Expand Down Expand Up @@ -37,4 +38,6 @@ void node_render(Node *node, Point camPos);

void node_free(Node *node);

bool node_is_over(Node *node, Point p);

#endif //HOMEWORK_NODE_H
2 changes: 2 additions & 0 deletions Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <stdio.h>

#define log_error(fmt, ...) (log_error_base(__FILE__, __LINE__, fmt, ##__VA_ARGS__))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

SDL_Surface *get_rgba_surface(int w, int h);
char *file_from_path(char *path);
Expand Down
21 changes: 12 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ int main(int argc, char **argv) {

NodeVector vec = nodev_create(0);

NSliceTexture texture = guigfx_create_nslice("res/GUI/Test.png", ST_REPEAT, 64, 64, 128, 128, renderer);
nodev_push_back(&vec, node_create_switch((Point){0, 0}, renderer));

nodev_update(&vec);

NSliceTexture texture = guigfx_create_nslice("res/GUI/Test.png", ST_REPEAT, 64, 64, 128, 128, renderer);

SDL_Cursor *cursor = SDL_GetCursor();
Camera camera;
camera.position = (Point){0, 0};
Expand Down Expand Up @@ -57,7 +59,10 @@ int main(int argc, char **argv) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);

guigfx_render_9slice(&texture, (SDL_Rect){50, 50, 400, 400}, renderer);
//guigfx_render_9slice(&texture, (SDL_Rect){0, 0, input_get_mouse_x(), input_get_mouse_y()}, renderer);

Point p = camera_screen_to_view(&camera, input_get_mouse_pos());
SDL_Point sp = {p.x, p.y};

SDL_SetRenderDrawColor(renderer,255,0, 0, 255);

Expand All @@ -80,13 +85,11 @@ int main(int argc, char **argv) {
SDL_CaptureMouse(false);
}

for (int i = 0; i < 3; i++) {
if (input_get_key(SDL_SCANCODE_1 + i).isPressed) {
vec.nodes[i].outValues[0] = !vec.nodes[i].outValues[0];
vec.nodes[i].dirty = true;
nodev_update(&vec);
}
}
if (node_is_over(&vec.nodes[0], p))
vec.nodes[0].outValues[0] = !vec.nodes[0].outValues[0];

SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderDrawPoint(renderer, p.x, p.y);

camera_zoom(&camera, (float)input_get_mouse_wheel_y(), input_get_mouse_pos());
SDL_RenderSetScale(renderer, camera.zoom, camera.zoom);
Expand Down
Binary file added res/GUI/Test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 168a4c8

Please sign in to comment.