This commit is contained in:
Barra Ó Catháin 2023-02-21 20:35:51 +00:00
parent 646ef07aab
commit b6663b3585
14 changed files with 193 additions and 5 deletions

View File

Before

Width:  |  Height:  |  Size: 613 B

After

Width:  |  Height:  |  Size: 613 B

View File

Before

Width:  |  Height:  |  Size: 613 B

After

Width:  |  Height:  |  Size: 613 B

View File

@ -1,14 +1,9 @@
* Experiments 1-3: Basics:
These experiments are mostly about learning to put things on the screen.
- Experiment 01: A "Radar" which tracks the position of the mouse, drawn using - Experiment 01: A "Radar" which tracks the position of the mouse, drawn using
SDL2 primitives and a loaded image. SDL2 primitives and a loaded image.
- Experiment 02: A stripped down version of the first experiment, using keyboard - Experiment 02: A stripped down version of the first experiment, using keyboard
input to move the cursor. input to move the cursor.
- Experiment 03: An improved version of 02, which uses movement acceleration to - Experiment 03: An improved version of 02, which uses movement acceleration to
make it feel better to use. make it feel better to use.
* Experiment 4-8: Gravity
These experiments are based around developing a gravity simulation.
- Experiment 04: An example of simple gravity and bouncing simulation. - Experiment 04: An example of simple gravity and bouncing simulation.
- Experiment 05: A "Spacewar" style gravity simulation, with a ship orbiting a star. - Experiment 05: A "Spacewar" style gravity simulation, with a ship orbiting a star.
- Experiment 06: A modified version of 05, using delta-time scaling so it is - Experiment 06: A modified version of 05, using delta-time scaling so it is
@ -19,3 +14,9 @@ These experiments are based around developing a gravity simulation.
"star." Fun to play aronud with. "star." Fun to play aronud with.
- Experiment 09: A version of the simulation where the ship can be steered and - Experiment 09: A version of the simulation where the ship can be steered and
accelerated. accelerated.
- Experiment 10: A version of the simulation where the ship has been given a
sprite.
- Experiment 11: A version of the simulation where the sprite animates while
accelerating.
- Experiment 12: A version of the simulation where the screen scrolls around
with the movement of the ship.

View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
typedef struct position
{
int x;
int y;
} position;
int main()
{
// Create the socket:
int socketFileDesc = socket(AF_INET, SOCK_DGRAM, 0);
if (socketFileDesc < 0)
{
fprintf(stderr, "\tSocket Creation is:\t\033[33;40mRED.\033[0m Aborting launch.\n");
exit(0);
}
printf("\tSocket Creation is:\t\033[32;40mGREEN.\033[0m\n");
// Create and fill the information needed to bind to the socket:
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET; // IPv4
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddress.sin_port = htons(12000);
position posToSend = {0, 0};
// Send data at the server:
while (1)
{
scanf("%d %d", &posToSend.x, &posToSend.y);
sendto(socketFileDesc, &posToSend, sizeof(posToSend), 0, (const struct sockaddr *)&serverAddress, sizeof(serverAddress));
}
}

View File

@ -0,0 +1,147 @@
// SDL/UDP Experiment 01, Barra Ó Catháin.
// =======================================
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
void DrawCircle(SDL_Renderer * renderer, int32_t centreX, int32_t centreY, int32_t radius)
{
const int32_t diameter = (radius * 2);
int32_t x = (radius - 1);
int32_t y = 0;
int32_t tx = 1;
int32_t ty = 1;
int32_t error = (tx - diameter);
while (x >= y)
{
// Each of the following renders an octant of the circle
SDL_RenderDrawPoint(renderer, centreX + x, centreY - y);
SDL_RenderDrawPoint(renderer, centreX + x, centreY + y);
SDL_RenderDrawPoint(renderer, centreX - x, centreY - y);
SDL_RenderDrawPoint(renderer, centreX - x, centreY + y);
SDL_RenderDrawPoint(renderer, centreX + y, centreY - x);
SDL_RenderDrawPoint(renderer, centreX + y, centreY + x);
SDL_RenderDrawPoint(renderer, centreX - y, centreY - x);
SDL_RenderDrawPoint(renderer, centreX - y, centreY + x);
if (error <= 0)
{
++y;
error += ty;
ty += 2;
}
if (error > 0)
{
--x;
tx += 2;
error += (tx - diameter);
}
}
}
// Get the largest radius for a circle that can fit in the width and height of a rectangle:
static inline int getRadius(int width, int height)
{
return (width/2 < height/2) ? width/2 : height/2;
}
int main(int argc, char ** argv)
{
SDL_Event event;
int positionX = 0, positionY = 0;
uint32_t rendererFlags = SDL_RENDERER_ACCELERATED;
// Initialize the SDL library, video, sound, and input:
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
printf("SDL Initialization Error: %s\n", SDL_GetError());
}
// Create an SDL window and rendering context in that window:
SDL_Window * window = SDL_CreateWindow("SDL_TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 640, 0);
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, rendererFlags);
// Enable resizing the window:
SDL_SetWindowResizable(window, SDL_TRUE);
// Create the socket:
int socketFileDesc = socket(AF_INET, SOCK_DGRAM, 0);
if (socketFileDesc < 0)
{
fprintf(stderr, "\tSocket Creation is:\t\033[33;40mRED.\033[0m Aborting launch.\n");
exit(0);
}
printf("\tSocket Creation is:\t\033[32;40mGREEN.\033[0m\n");
// Make the socket timeout:
struct timeval read_timeout;
read_timeout.tv_sec = 0;
read_timeout.tv_usec = 10;
setsockopt(socketFileDesc, SOL_SOCKET, SO_RCVTIMEO, &read_timeout, sizeof read_timeout);
// Create and fill the information needed to bind to the socket:
struct sockaddr_in serverAddress;
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET; // IPv4
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(12000);
// Bind to the socket:
if (bind(socketFileDesc, (const struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
// A struct to transfer the position data:
typedef struct position
{
int x;
int y;
} position;
position currentPosition = {0, 0};
while (true)
{
// Receive data from the socket:
recvfrom(socketFileDesc, &currentPosition, sizeof(position), 0, NULL, NULL);
// Set the position to the received one:
positionX = currentPosition.x;
positionY = currentPosition.y;
// Set the colour to black:
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
// Clear the screen, filling it with black:
SDL_RenderClear(renderer);
// Set the colour to yellow:
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
// Draw a circle around the position pointer:
DrawCircle(renderer, positionX, positionY, 15);
// Present the rendered graphics:
SDL_RenderPresent(renderer);
// Delay enough so that we run at 144 frames:
SDL_Delay(1000 / 144);
}
return 0;
}
// =============================================================================
// Local Variables:
// compile-command: "gcc `sdl2-config --libs --cflags` SDL2-UDP-Experiment-01.c"
// End: