summaryrefslogtreecommitdiffstats
path: root/nyan.c
blob: ca5f1f21942b61a2ad17166451348c87fa4db108 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
	/* nyancat in GNU C
	Copyright © 2024 DiffieHellman

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as
	published by the Free Software Foundation, either version 3 of the
	License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.

	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
	*/

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <stdbool.h>

#include "nyan.h"
#include <bsd/sys/queue.h> // linked list implementation
#include "main.h"

void add_sparkle(struct head_sparkle *sparkle_list)
	{
	sparkle_instance *new = ec_malloc(sizeof(sparkle_instance));

	new->loc.x = screen_width + 80;
	new->loc.y = (rand() % (screen_height + sparkle_height)) - sparkle_height;
	new->frame = 0;
	new->frame_mov = 1;
	new->speed = 10 + (rand() % 30);
	new->layer = rand() % 2;

	LIST_INSERT_HEAD(sparkle_list, new, entries);
	}

void add_cat(unsigned int x, unsigned int y, int bx, int by, struct head_cat *cat_list)
	{
	cat_instance *new = ec_malloc(sizeof(cat_instance));

	new->loc.x = x;
	new->loc.y = y;

	new->bounce_direction.x = bx;
	new->bounce_direction.y = by;

	LIST_INSERT_HEAD(cat_list, new, entries);
	}

void add_rainbow(unsigned int x, unsigned int y, struct head_rainbow *rainbow_list)
	{
	rainbow_instance *new = ec_malloc(sizeof(rainbow_instance));

	new->loc.x = x;
	new->loc.y = y;
	new->sprite = rainbow_sprite;

	LIST_INSERT_HEAD(rainbow_list, new, entries);
	}

void update_rainbows(struct head_cat *cat_list, struct head_rainbow *rainbow_list)
	{
	cat_instance *c;

	/* rainbows need to be placed according to the cat position (which changes constantly) */
	LIST_FOREACH(c, cat_list, entries)
		{
		add_rainbow((screen_width - rainbow_width) / 2 - OFFSET	 /* Default position in the center */
			- ((screen_width - cat_width) / 2 - c->loc.x),	 /* Plus the amount the cat is off the center */
			(screen_height - rainbow_height) / 2		 /* Default position in the center */
			- ((screen_height - cat_height) / 2 - c->loc.y), rainbow_list);/* Plus the amount the cat is off the center */
		}


	rainbow_instance *r, *rtmp;

	/* update the position of each rainbow left */
	LIST_FOREACH_SAFE(r, rainbow_list, entries, rtmp)
		{
		r->loc.x -= rainbow_width;

		/* if rainbow is off screen, delete and free() it */
		if ((r->loc.x + rainbow_width) < 0)
			{
			LIST_REMOVE(r, entries);
			free(r);
			}
		}
	}

void update_sparkles(struct head_sparkle *sparkle_list)
	{

	/* ensure screen has enough sparkles at all times */
	sparkle_spawn_counter += rand() % screen_height;
	while (sparkle_spawn_counter >= 1000)
		{
		add_sparkle(sparkle_list);
		sparkle_spawn_counter -= 1000;
		}

	sparkle_instance *s, *stmp;
	LIST_FOREACH_SAFE(s, sparkle_list, entries, stmp)
		{
		/* move sparkle left */
		s->loc.x -= s->speed;
		s->frame += s->frame_mov;

		/* reset sparkle frame */
		if (s->frame + 1 >= sparkle_count || s->frame < 1){s->frame_mov = 0 - s->frame_mov;}

		/* if sparkle offscreen, delete it */
		if ((s->loc.x + sparkle_width) < 0)
			{
			LIST_REMOVE(s, entries);
			free(s);
			}
		}
	}

void handle_sine(struct head_cat *cat_list)
	{
	cat_instance *c;

	LIST_FOREACH(c, cat_list, entries)
		{
		/* sine magic */
		double pos = (screen_height - cat_height)/2 * sin((2*PI*444444)*t);
		c->loc.y = ((screen_height - cat_height)/2 - pos);

//		double posx = (screen_width - cat_height)/2 * sin((2*PI*444444)*t);
//		c->loc.x = ((screen_width - cat_height)/2 - posx);
		}

	t += 20;
	}

void handle_bounce(struct head_cat *cat_list)
	{
	cat_instance *c;

	LIST_FOREACH(c, cat_list, entries)
		{
		/* bounce magic */
		if (c->loc.x < 0 || c->loc.x > (screen_width-cat_width)){c->bounce_direction.x = -(c->bounce_direction.x);}
		if (c->loc.y < 0 || c->loc.y > (screen_height-(cat_width/2))){c->bounce_direction.y = -(c->bounce_direction.y);}
		c->loc.x += (c->bounce_direction.x * (screen_width/64));
		c->loc.y += (c->bounce_direction.y * (screen_height/64));
		}
	}

void draw_cats(struct head_cat *cat_list)
	{
	cat_instance* c;

	LIST_FOREACH(c, cat_list, entries)
		{
		/* source sprite dimensions */
		SDL_Rect srcrect = {cat_sprite * cat_width, 0, cat_width, cat_height};
		/* dimensions of destination rectangle */
                SDL_Rect dstrect = {c->loc.x, c->loc.y, cat_width*cat_size, cat_height*cat_size};

		SDL_RenderCopy(renderer, cat_texture, &srcrect, &dstrect);
		}
	}

void draw_rainbows(struct head_rainbow *rainbow_list)
	{
	rainbow_instance* r;

	LIST_FOREACH(r, rainbow_list, entries)
		{
		/* source sprite dimensions */
		SDL_Rect srcrect = {r->sprite * rainbow_width, 0, rainbow_width, rainbow_height};
		/* dimensions of destination rectangle */
		SDL_Rect dstrect = {r->loc.x, r->loc.y, rainbow_width*cat_size, rainbow_height*cat_size};

		SDL_RenderCopy(renderer, rainbow_texture, &srcrect, &dstrect);
		}
	}

void draw_sparkles(struct head_sparkle *sparkle_list)
	{
	sparkle_instance* s;

	LIST_FOREACH(s, sparkle_list, entries)
		{
		/* source sprite dimensions */
		SDL_Rect srcrect = {sparkle_sprite * sparkle_width, 0, sparkle_width, sparkle_height};
		/* dimensions of destination rectangle */
                SDL_Rect dstrect = {s->loc.x, s->loc.y, sparkle_width*cat_size, sparkle_height*cat_size};

		SDL_RenderCopy(renderer, sparkle_texture, &srcrect, &dstrect);
		}
	}