Random Stuff

This commit is contained in:
0xmac 2025-02-14 12:59:12 +01:00
commit 5f398b7d44
4 changed files with 485 additions and 0 deletions

57
17012025.c Normal file
View File

@ -0,0 +1,57 @@
#include <inttypes.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "libmatrix.h"
#include "mathshell.c"
#define HELP "\nUsage: a.out [options]\n\n\
Options: Type Default Desc\n\
--help Show this menu\n\
--fps [float] 30 Set the FPS and Updating interval\n\
\n"
uint8_t roundOut = 0;
void fetchArgs(int argc, const char *argv[]){
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--help")) { printf(HELP); exit(EXIT_SUCCESS); }
else if (!strcmp(argv[i], "--round-output")) roundOut = 1;
else {
printf("Unknown parameter '%s'\nTry '--help' for help\n", argv[i]);
exit(EXIT_FAILURE);
}
}
}
int main(int argc, const char *argv[]){
printf("A Math program to test libmatrix.c\n\n");
fetchArgs(argc, argv);
struct matrix *a = &(struct matrix) {};
matrixNewFromUser(a,3,3);
printf("Det: %f\n", matrixDet(a));
//matrixPrint(a, "a");
struct matrix *i = &(struct matrix) {};
matrixNew(i, 3, 3);
matrixIdentity(i);
matrixMultiplyByNumber(i, 4);
matrixPrint(i, "I");
struct matrix *out = &(struct matrix) {};
matrixMultiply(a, i, out);
matrixPrint(out, "a * I");
//printf("%d\n", stringHash("Hello"));
//initShell();
return EXIT_SUCCESS;
}

198
libmatrix.c Normal file
View File

@ -0,0 +1,198 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAXSTEPS 1000000
extern uint8_t roundOut;
static uint32_t stepCount;
struct matrix {
uint16_t width;
uint16_t height;
float **data;
};
void haltAndCatchFire(const char* message){
printf("%s\n", message);
exit(EXIT_FAILURE);
}
void matrixIsQuadratical(struct matrix *m){
if ((uint16_t)m->width != (uint16_t)m->height){
printf("Array size is not quadratical!\n");
exit(EXIT_FAILURE);
}
}
void matrixNew(struct matrix *dest, uint16_t size_x, uint16_t size_y){
/* Set size constants */
dest->width = size_x;
dest->height = size_y;
dest->data = malloc(size_y * sizeof(intptr_t));
for (int i = 0; i < size_y; i++){
dest->data[i] = malloc(size_x * sizeof(float));
for (int j = 0; j < size_x; j++) dest->data[i][j] = 0;
}
if (dest == NULL || dest->data == NULL){
haltAndCatchFire("Failed to init matrix!");
}
}
void maxtrixFree(struct matrix *m){
for (int i = 0; i < m->height; i++){
free(m->data[i]);
}
free(m->data);
free(m);
}
void matrixNewFromUser(struct matrix *dest, uint16_t size_x, uint16_t size_y){
matrixNew(dest, size_x, size_y);
printf("Please enter a %dx%d array:\n [\n", dest->width, dest->height);
float value = 0;
for (int y = 0; y < size_y; y++){
printf(" ");
for (int x = 0; x < size_x; x++){
if (fscanf(stdin, "%f", &value) != 1) haltAndCatchFire("Error while reading input!");
dest->data[y][x] = value;
}
}
printf(" ]\n");
}
void matrixPrint(struct matrix *m, const char* name){
printf("%s(%d, %d): \n [\n", name, m->width, m->height);
for (int y = 0; y < m->height; y++){
printf(" ");
for (int x = 0; x < m->width; x++){
if (roundOut) printf("%d ", (int)m->data[y][x]);
else printf("%f ", m->data[y][x]);
}
printf("\n");
}
printf(" ]\n\n");
}
void matrixIdentity(struct matrix *m){
matrixIsQuadratical(m);
for (int y = 0; y < m->height; y++){
for (int x = 0; x < m->width; x++){
/* If x == y, then put a 1, else a 0 */
m->data[y][x] = x == y ? 1 : 0;
}
}
}
void matrixFill(struct matrix *m, float value){
for (int y = 0; y < m->height; y++){
for (int x = 0; x < m->width; x++){
m->data[y][x] = value;
}
}
}
float matrixDet(struct matrix *m){
matrixIsQuadratical(m);
/*
float* workMatrix = malloc((size_x + 1) * sizeof(float));
for (int i = 1; i <= size_x; i++) workMatrix[i] = i % 2 ? matrix[1][i] : -matrix[1][i];
*/
float result = 0;
for (int column = 0; column < 3; column++){
float val = (m->data[1][((column + 1) % 3)] * m->data[2][((column + 2 ) % 3)]) - (m->data[2][((column + 1) % 3)] * m->data[1][((column + 2 ) % 3)]);
result += (column != 1 ? m->data[0][column] : -m->data[0][column]) * (column != 1 ? val : -val);
}
return result;
}
/*
float matrixDet(const float *matrix, uint16_t start_x, uint16_t start_y, int16_t size){
float val;
for (int y = start_y; y < start_y + size; y++){
for (int x = start_x; x < start_x + size; x++){
if (stepCount > MAXSTEPS){
printf("Exceeded maximum calculation iteration safeguard of > %d\n", MAXSTEPS);
exit(EXIT_FAILURE);
}
stepCount++;
}
}
return val;
}*/
void matrixRound(struct matrix *m){
for (int y = 0; y < m->height; y++){
for (int x = 0; x < m->width; x++){
m->data[y][x] = roundf(m->data[y][x]);
}
}
}
void matrixAdd(struct matrix *a, struct matrix *b, struct matrix *dest){
if (a->width != b->width || a->height != b->height){
printf("Matricies are not equal in size!");
exit(EXIT_FAILURE);
}
//matrixFree(dest);
matrixNew(dest, a->width, a->height);
for (int y = 0; y < a->height; y++){
for (int x = 0; x < a->width; x++){
dest->data[y][x] = a->data[y][x] + b->data[y][x];
}
}
}
void matrixMultiply(struct matrix *a, struct matrix *b, struct matrix *dest){
if (a->width != b->height) haltAndCatchFire("Matricies are not compatible for multiplication!");
matrixNew(dest, b->width, a->height);
for (int y = 0; y < a->height; y++){
for (int x = 0; x < b->width; x++){
for (int i = 0; i < a->width; i++){
dest->data[y][x] += (a->data[y][i] * b->data[i][x]);
}
}
}
}
void matrixMultiplyByNumber(struct matrix *m, float value){
for (int y = 0; y < m->height; y++){
for (int x = 0; x < m->width; x++){
m->data[y][x] = m->data[y][x] * value;
}
}
}
void matrixPow(float *matrix, float value){
}
void matrixTranspose(float *matrix){
}

112
libmatrix.h Normal file
View File

@ -0,0 +1,112 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "libmatrix.c"
/* HaltAndCatchFire
*
* displays a custom errormassage and exits the program
*/
void haltAndCatchFire(const char* message);
/* MatrixIsQuadratical
*
* Checks if width == height, else complaints and exits the progam
*/
void matrixIsQuadratical(struct matrix *m);
/* MatrixNew
*
* Allocates a new matrix by size x and y
*/
void matrixNew(struct matrix *dest, uint16_t size_x, uint16_t size_y);
/* MatrixFree
*
* Free's a matrix'es array and itself
*/
void maxtrixFree(struct matrix *m);
/* MatrixNewFromUser
*
* Same as MatrixNew, but queries the user
* for input to fill the matrix
*/
void matrixNewFromUser(struct matrix *dest, uint16_t size_x, uint16_t size_y);
/* MatrixPrint
*
* Prints the given matrix in a nice format
* and a custom given display name
*/
void matrixPrint(struct matrix *m, const char* name);
/* MatrixIdentity
*
* Creates a Identitiy matrix, if the matrix is quadratical
*/
void matrixIdentity(struct matrix *m);
/* MatrixFill
*
* Fills the matrix with a value
*/
void matrixFill(struct matrix *m, float value);
/* MatrixDet
*
* Calculated the determinant of the matrix
*/
float matrixDet(struct matrix *m);
/* MatrixRound
*
* Rounds every entry in the matrix
*/
void matrixRound(struct matrix *m);
/* MatrixAdd
*
* Adds two matricies together and store it in dest
* dest get's initialized with the size of a
*
* a and b have to be equal in size
*/
void matrixAdd(struct matrix *a, struct matrix *b, struct matrix *dest);
/* MatrixMultiply
*
* Multiply two matricies together and store it in dest
* dest get's initialized with the size of a.height, b.with
*
* a and b have to be compatible for multiplication
*/
void matrixMultiply(struct matrix *a, struct matrix *b, struct matrix *dest);
/* MatrixMultiplyByNumber
*
* Multiply the entire array by a fixed value
*/
void matrixMultiplyByNumber(struct matrix *m, float value);
void matrixPow(float *matrix, float value);
void matrixTranspose(float *matrix);

118
sdlinit.c Normal file
View File

@ -0,0 +1,118 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
extern void keydownHandler(SDL_Scancode);
/* Predefined Screen Size */
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
static float sdlUpdate = 33.f;
/* Video Memory */
static uint32_t videoMemory[SCREEN_WIDTH * SCREEN_HEIGHT];
/* SDL static stuff */
static SDL_Window *window;
static SDL_Renderer *renderer;
static SDL_Texture *texture;
static SDL_Cursor *cursor;
static SDL_Event event;
/* Function to init the SDL Window and dependencies */
void initWindow(){
if (SDL_Init(SDL_INIT_VIDEO)){
printf("Could not init SDL: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
/* When we exit(CODE), then SDL_Quit is ran */
atexit(SDL_Quit);
/* Init Window */
window = SDL_CreateWindow("Program",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, 0);
if (window == NULL){
printf("Fatal! Could not create SDL Window: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
/* Init Renderer */
renderer = SDL_CreateRenderer(window, -1, 2);
if (renderer == NULL){
printf("Fatal! Could not create SDL Renderer: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
/* Init Texture */
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
SCREEN_WIDTH, SCREEN_HEIGHT);
if (texture == NULL){
printf("Fatal! Could not create SDL Texture: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE)){
printf("Fatal! Could not set SDL blend mode: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
/* Init the Cursor */
cursor = SDL_CreateSystemCursor(0);
if (cursor == NULL){
printf("Fatal! Could not create SDL cursor: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_SetCursor(cursor);
}
/* Clear the framebuffer with a color */
void clearScreen(uint32_t pixels[], uint32_t color){
for (uint32_t i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) pixels[i] = color;
}
/* Get next events and do sm with it */
void pollEvents(){
while (SDL_PollEvent(&event)){
switch(event.type){
/* If Quit, we quit the Application lul */
case SDL_QUIT:
printf("Quit at users request\n");
exit(EXIT_SUCCESS);
break;
/* Fetch pressed Keys */
case SDL_KEYDOWN:
switch (event.key.keysym.scancode){
case SDL_SCANCODE_ESCAPE:
printf("Quit at users request\n");
exit(0);
break;
default:
keydownHandler(event.key.keysym.scancode);
break;
}
break;
default:
break;
}
}
}
/* Update SDL Texture and Renderer with framebuffer */
void updateSDL(){
if (SDL_UpdateTexture(texture, NULL, videoMemory, (sizeof(uint32_t) * SCREEN_WIDTH))) {
fprintf(stderr, "Could not update SDL texture: %s.\n", SDL_GetError());
exit(EXIT_FAILURE);
}
if (SDL_RenderCopy(renderer, texture, NULL, NULL)) {
fprintf(stderr, "Could not display SDL texture: %s.\n", SDL_GetError());
exit(EXIT_FAILURE);
}
}