Skip to content

Commit

Permalink
Improved sine effect, added visual debug
Browse files Browse the repository at this point in the history
  • Loading branch information
twojstaryzdomu committed May 30, 2023
1 parent 409d2fb commit f579d42
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 17 deletions.
95 changes: 78 additions & 17 deletions sys_sdl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ static SDL_Joystick *_joystick;
static int _controller_up;
static bool _controller_up_setup;

static uint16_t _sine_index;
static int16_t _sine_index;
static int _sine_direction;
static bool _sine_plot;
static int8_t _sine_offset_y;
static uint16_t _sine_scale_x;
static uint16_t _sine_scale_y, _orig_sine_scale_y;

static int sdl2_init() {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER | (g_sys.audio ? SDL_INIT_AUDIO : 0));
Expand Down Expand Up @@ -239,6 +244,11 @@ static void sdl2_set_screen_size(int w, int h, const char *caption, int scale, c
_sprites_cliprect.w = g_sys.w;
_sprites_cliprect.h = g_sys.h;
_sine_index = 0;
_sine_direction = 1;
_sine_offset_y = ORIG_H / 2 - sine_tbl[0];
_sine_scale_x = 2;
_sine_scale_y = 10;
_orig_sine_scale_y = _sine_scale_y;
g_sys.hybrid_color = hybrid_color;
}

Expand Down Expand Up @@ -563,6 +573,44 @@ static void sdl2_print_palette() {
}
}

static void sdl2_sine_screen() {
uint16_t sine_x = _sine_index;
int16_t sine_y = ((sine_tbl[_sine_index]) + _sine_offset_y) * _sine_scale_y / 10 * _sine_scale_x;
SDL_Rect s1 = { .x = _centred_x_offset + MAX(0, -sine_x), .y = _centred_y_offset + MAX(0, -sine_y), .w = ORIG_W - abs(sine_x), .h = ORIG_H - abs(sine_y) };
SDL_Rect d1 = { .x = _centred_x_offset + MAX(0, sine_x), .y = _centred_y_offset + MAX(0, sine_y), .w = s1.w, .h = s1.h };
SDL_Rect s2 = { .x = sine_x > 0 ? s1.x + s1.w : _centred_x_offset, .y = sine_y > 0 ? s1.y + s1.h : _centred_y_offset, .w = ORIG_W - s1.w, .h = ORIG_H - s1.h };
SDL_Rect d2 = { .x = _centred_x_offset, .y = sine_y < 0 ? d1.h + d1.y : _centred_y_offset, .w = sine_x, .h = s2.h };
SDL_Rect s3 = { .x = s1.x, .y = sine_y > 0 ? s1.y + s1.h : _centred_y_offset, .w = s1.w, .h = ORIG_H - s1.h };
SDL_Rect d3 = { .x = _centred_x_offset + MAX(0, sine_x), .y = d2.y, .w = d1.w, .h = ORIG_H - d1.h };
SDL_Rect s4 = { .x = s1.x + s1.w, .y = s1.y, .w = ORIG_W - s1.w, .h = s1.h };
SDL_Rect d4 = { .x = _centred_x_offset, .y = _centred_y_offset + MAX(0, sine_y), .w = sine_x, .h = d1.h };
print_debug(DBG_SYSTEM, "Sine wave: %d, %d @ %d "
"s1: %d,%d - %d,%d "
"d1: %d,%d - %d,%d "
"s1.h = %d; s1.y = %d; d1.h = %d; d1.y = %d; dir = %d",
sine_x, sine_y, _sine_index,
s1.x, s1.y, s1.w + s1.x, s1.h + s1.y,
d1.x, d1.y, d1.w + d1.x, d1.h + d1.y,
s1.h, s1.y, d1.h, d1.y, _sine_direction);
SDL_RenderCopy(_renderer, _texture, &s1, &d1);
SDL_RenderCopy(_renderer, _texture, &s2, &d2);
SDL_RenderCopy(_renderer, _texture, &s3, &d3);
SDL_RenderCopy(_renderer, _texture, &s4, &d4);
if (_sine_plot) {
SDL_SetRenderDrawColor(_renderer, 255, 0, 0, 255);
uint16_t x;
int16_t sine_plot_y;
for (x = 0; x <= _sine_index; x++) {
sine_plot_y = ((sine_tbl[x]) + _sine_offset_y) * _sine_scale_y / 10 * _sine_scale_x;
SDL_RenderDrawPoint(_renderer, x + _centred_x_offset, sine_plot_y + _centred_y_offset);
}
print_debug(DBG_SYSTEM, "Sine plot: %d, %d _sine_scale_x %d _sine_scale_y %d _sine_offset_y %d sine_tbl(x) %d",
x, sine_plot_y, _sine_scale_x, _sine_scale_y / 10, _sine_offset_y, sine_tbl[x]);
}
SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 0);
_sine_index = (_sine_index + _sine_direction + ORIG_W) % ORIG_W;
}

static void sdl2_update_screen_cached(const uint8_t *p, int present, bool cache_redraw) {
if (!cache_redraw) {
if (_copper_color_key != -1) {
Expand Down Expand Up @@ -604,22 +652,7 @@ static void sdl2_update_screen_cached(const uint8_t *p, int present, bool cache_
dst = &r;
}
if (g_sys.sine) {
uint16_t sine_x = _sine_index % ORIG_W;
int sine_y = ((int8_t)sin_tbl_sys[_sine_index % sizeof(sin_tbl_sys)]) + sin_tbl_sys[0];
SDL_Rect s1 = { .x = _centred_x_offset + MAX(0, -sine_x), .y = _centred_y_offset + MAX(0, -sine_y), .w = ORIG_W - sine_x, .h = ORIG_H - sine_y };
SDL_Rect d1 = { .x = _centred_x_offset + MAX(0, sine_x), .y = _centred_y_offset + MAX(0, sine_y), .w = ORIG_W - abs(sine_x), .h = ORIG_H - abs(sine_y) };
print_debug(DBG_SYSTEM, "Sine wave: %d, %d @ %d s1: %d,%d - %d,%d, d1: %d,%d - %d,%d", sine_x, sine_y, _sine_index, s1.x, s1.y, s1.w + s1.x, s1.h + s1.y, d1.x, d1.y, d1.w + d1.x, d1.h + d1.y);
SDL_RenderCopy(_renderer, _texture, &s1, &d1);
SDL_Rect s2 = { .x = s1.x + s1.w, .y = s1.y + s1.h, .w = ORIG_W - s1.w, .h = ORIG_H - s1.h };
SDL_Rect d2 = { .x = _centred_x_offset, .y = _centred_y_offset, .w = sine_x, .h = sine_y };
SDL_RenderCopy(_renderer, _texture, &s2, &d2);
SDL_Rect s3 = { .x = s1.x, .y = s1.y + s1.h, .w = s1.w, .h = ORIG_H - s1.h };
SDL_Rect d3 = { .x = _centred_x_offset + MAX(0, sine_x), .y = _centred_y_offset, .w = d1.w, .h = sine_y };
SDL_RenderCopy(_renderer, _texture, &s3, &d3);
SDL_Rect s4 = { .x = s1.x + s1.w, .y = s1.y, .w = ORIG_W - s1.w, .h = s1.h };
SDL_Rect d4 = { .x = _centred_x_offset, .y = _centred_y_offset + MAX(0, sine_y), .w = sine_x, .h = d1.h };
SDL_RenderCopy(_renderer, _texture, &s4, &d4);
_sine_index += 1;
sdl2_sine_screen();
} else
SDL_RenderCopy(_renderer, _texture, src, dst);
sdl2_update_sprites_screen();
Expand Down Expand Up @@ -752,6 +785,26 @@ static void handle_keyevent(const SDL_Keysym *keysym, bool keydown, struct input
g_message.add("%sabled debug %lu", debug_enabled ? "Dis" : "En", debug_level);
}
break;
case SDLK_KP_2:
if (keydown)
_sine_offset_y++;
break;
case SDLK_KP_4:
if (keydown) {
_sine_scale_y += _orig_sine_scale_y / 10;
_sine_offset_y -= 2 * _orig_sine_scale_y / 10;
}
break;
case SDLK_KP_6:
if (keydown) {
_sine_scale_y -= _orig_sine_scale_y / 10;
_sine_offset_y += 2 * _orig_sine_scale_y / 10;
}
break;
case SDLK_KP_8:
if (keydown)
_sine_offset_y--;
break;
case SDLK_MINUS:
if (keydown) {
g_sys.palette_offset = -1;
Expand Down Expand Up @@ -822,6 +875,10 @@ static void handle_keyevent(const SDL_Keysym *keysym, bool keydown, struct input
_controller_up_setup = true;
}
break;
case SDLK_k:
if (keydown)
_sine_plot = !_sine_plot;
break;
case SDLK_l:
if (keydown)
sdl2_rehint_screen("linear");
Expand All @@ -848,6 +905,10 @@ static void handle_keyevent(const SDL_Keysym *keysym, bool keydown, struct input
SDL_PauseAudio(*paused);
}
break;
case SDLK_q:
if (keydown)
_sine_direction = -_sine_direction;
break;
case SDLK_s:
if (keydown) {
_size_lock = !_size_lock;
Expand Down
19 changes: 19 additions & 0 deletions sys_sine.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
#include <sys_sine.h>

const uint8_t sine_tbl[] = {
0xC8,0xC7,0xC7,0xC7,0xC7,0xC7,0xC7,0xC7,0xC6,0xC6,0xC6,0xC5,0xC5,0xC4,0xC4,0xC3,0xC3,0xC2,0xC1,0xC1,
0xC0,0xBF,0xBE,0xBD,0xBD,0xBC,0xBB,0xBA,0xB9,0xB8,0xB7,0xB5,0xB4,0xB3,0xB2,0xB1,0xAF,0xAE,0xAD,0xAB,
0xAA,0xA9,0xA7,0xA6,0xA4,0xA3,0xA1,0xA0,0x9E,0x9C,0x9B,0x99,0x97,0x96,0x94,0x92,0x91,0x8F,0x8D,0x8B,
0x89,0x88,0x86,0x84,0x82,0x80,0x7E,0x7C,0x7A,0x79,0x77,0x75,0x73,0x71,0x6F,0x6D,0x6B,0x69,0x67,0x65,
0x63,0x61,0x5F,0x5D,0x5B,0x59,0x57,0x55,0x53,0x51,0x4F,0x4E,0x4C,0x4A,0x48,0x46,0x44,0x42,0x40,0x3E,
0x3D,0x3B,0x39,0x37,0x36,0x34,0x32,0x30,0x2F,0x2D,0x2B,0x2A,0x28,0x27,0x25,0x24,0x22,0x21,0x1F,0x1E,
0x1C,0x1B,0x1A,0x18,0x17,0x16,0x14,0x13,0x12,0x11,0x10,0x0F,0x0E,0x0D,0x0C,0x0B,0x0A,0x09,0x08,0x08,
0x07,0x06,0x05,0x05,0x04,0x04,0x03,0x03,0x02,0x02,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x02,0x02,0x03,0x03,0x04,0x04,0x05,0x05,0x06,0x07,
0x08,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13,0x14,0x16,0x17,0x18,0x1A,0x1B,0x1C,
0x1E,0x1F,0x21,0x22,0x24,0x25,0x27,0x28,0x2A,0x2B,0x2D,0x2F,0x30,0x32,0x34,0x36,0x37,0x39,0x3B,0x3D,
0x3E,0x40,0x42,0x44,0x46,0x48,0x4A,0x4C,0x4E,0x4F,0x51,0x53,0x55,0x57,0x59,0x5B,0x5D,0x5F,0x61,0x63,
0x65,0x67,0x69,0x6B,0x6D,0x6F,0x71,0x73,0x75,0x77,0x79,0x7A,0x7C,0x7E,0x80,0x82,0x84,0x86,0x88,0x89,
0x8B,0x8D,0x8F,0x91,0x92,0x94,0x96,0x97,0x99,0x9B,0x9C,0x9E,0xA0,0xA1,0xA3,0xA4,0xA6,0xA7,0xA9,0xAA,
0xAB,0xAD,0xAE,0xAF,0xB1,0xB2,0xB3,0xB4,0xB5,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBD,0xBE,0xBF,0xC0,
0xC1,0xC1,0xC2,0xC3,0xC3,0xC4,0xC4,0xC5,0xC5,0xC6,0xC6,0xC6,0xC7,0xC7,0xC7,0xC7,0xC7,0xC7,0xC7,0xC8
};

const uint8_t cos_tbl_sys[] = {
0x00,0x01,0x03,0x04,0x06,0x07,0x09,0x0A,0x0C,0x0E,0x0F,0x11,0x12,0x14,0x15,0x17,
0x18,0x19,0x1B,0x1C,0x1E,0x1F,0x20,0x22,0x23,0x24,0x26,0x27,0x28,0x29,0x2A,0x2C,
Expand Down
2 changes: 2 additions & 0 deletions sys_sine.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#define SYS_SINE_H__

#include <stdint.h>
#include "sys.h"

const uint8_t sine_tbl[ORIG_W];
const uint8_t cos_tbl_sys[256];
const uint8_t sin_tbl_sys[256];

Expand Down

0 comments on commit f579d42

Please sign in to comment.