VPN termux about China.net

Saturday 31 August 2024

tea.obj sdl2 c4droid



 #include <iostream>

#include <vector>

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <sys/mman.h>

#include <fcntl.h>

#include <unistd.h>


#include "SDL2/SDL.h"

#include "SDL_test_common.h"

#if defined(__IPHONEOS__) || defined(__ANDROID__)

#define HAVE_OPENGLES

#endif

#include "SDL_opengles.h"


struct mouse_handle {

    int x = 1;

    int y = 1;

} mouse;


int gl;


struct Vertex {

    float x, y, z;

};


static GLubyte color[8][4] = {{255, 0, 0, 0}};

std::vector<Vertex> vertices;


void glBegin(int gl) {

    vertices.clear();

}


void glVertex3f(float x, float y, float z) {

    vertices.push_back({x, y, z});

}


void glEnd() {

    switch (gl) {

        case GL_POINTS:

            for (const auto &vertex : vertices) {

                std::cout << "Point: (" << vertex.x << ", " << vertex.y << ", " << vertex.z << ")" << std::endl;

            }

            glEnableClientState(GL_COLOR_ARRAY);

            glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);

            glEnableClientState(GL_VERTEX_ARRAY);

            glVertexPointer(3, GL_FLOAT, sizeof(Vertex), (void*)vertices.data());

            glDrawArrays(GL_POINTS, 0, vertices.size());

            break;

        case GL_TRIANGLES:

            if (vertices.size() % 3 != 0) {

                std::cerr << "Warning: Number of vertices is not a multiple of 3 for GL_TRIANGLES" << std::endl;

            }

            for (size_t i = 0; i < vertices.size(); i += 3) {

                std::cout << "Triangle: (" << vertices[i].x << ", " << vertices[i].y << ", " << vertices[i].z << ") -> ("

                          << vertices[i + 1].x << ", " << vertices[i + 1].y << ", " << vertices[i + 1].z << ") -> ("

                          << vertices[i + 2].x << ", " << vertices[i + 2].y << ", " << vertices[i + 2].z << ")" << std::endl;

            }

            glEnableClientState(GL_COLOR_ARRAY);

            glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);

            glEnableClientState(GL_VERTEX_ARRAY);

            glVertexPointer(3, GL_FLOAT, sizeof(Vertex), (void*)vertices.data());

            glDrawArrays(GL_TRIANGLES, 0, vertices.size());

            break;

        case GL_LINES:

            if (vertices.size() % 2 != 0) {

                std::cerr << "Warning: Number of vertices is not a multiple of 2 for GL_LINES" << std::endl;

            }

            for (size_t i = 0; i < vertices.size(); i += 2) {

                std::cout << "Line: (" << vertices[i].x << ", " << vertices[i].y << ", " << vertices[i].z << ") -> ("

                          << vertices[i + 1].x << ", " << vertices[i + 1].y << ", " << vertices[i + 1].z << ")" << std::endl;

            }

            glEnableClientState(GL_COLOR_ARRAY);

            glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);

            glEnableClientState(GL_VERTEX_ARRAY);

            glVertexPointer(3, GL_FLOAT, sizeof(Vertex), (void*)vertices.data());

            glDrawArrays(GL_LINES, 0, vertices.size());

            break;

        default:

            std::cerr << "Unsupported primitive type!" << std::endl;

            break;

    }

}


GLuint car;

float carrot;


void loadObj(const char *fname) {

    int fd = open(fname, O_RDONLY);

    if (fd == -1) {

        perror("open");

        exit(EXIT_FAILURE);

    }


    off_t fileSize = lseek(fd, 0, SEEK_END);

    if (fileSize == -1) {

        perror("lseek");

        close(fd);

        exit(EXIT_FAILURE);

    }


    void *map = mmap(NULL, fileSize, PROT_READ, MAP_PRIVATE, fd, 0);

    if (map == MAP_FAILED) {

        perror("mmap");

        close(fd);

        exit(EXIT_FAILURE);

    }


    close(fd);


    const char *data = (const char *)map;

    const char *end = data + fileSize;


    glPointSize(2.0);

    glPushMatrix();

    glRotatef(mouse.x % 360, mouse.y % 360, mouse.x % 360, mouse.y % 360);

    glPointSize(10.0f);

    glBegin(GL_TRIANGLES);


    while (data < end) {

        if (*data == 'v' && *(data + 1) == ' ') {

            data += 2; // pomiń "v "

            float x, y, z;

            if (sscanf(data, "%f %f %f", &x, &y, &z) == 3) {

                glVertex3f(x, y, z);

            }

        }


        // Przejdź do następnej linii

        while (data < end && *data != '\n') {

            ++data;

        }

        ++data; // Pomiń nową linię

    }


    glEnd();

    glPopMatrix();


    if (munmap(map, fileSize) == -1) {

        perror("munmap");

    }

}

void setPerspective(float fov, float aspect, float znear, float zfar) {

    float ymax = znear * tanf(fov * M_PI / 360.0f);

    float ymin = -ymax;

    float xmin = ymin * aspect;

    float xmax = ymax * aspect;

    glFrustumf(xmin, xmax, ymin, ymax, znear, zfar);

}


SDLTest_CommonState *state;

SDL_Event *event;

static SDL_GLContext *context;


struct obj {

    float x;

    float y;

    float z;


    int Render() {

        glMatrixMode(GL_PROJECTION);

        glLoadIdentity();

        setPerspective(45.0f, 1.0f, 0.1f, 100.0f);

        glMatrixMode(GL_MODELVIEW);

        glLoadIdentity();

        glTranslatef(0.0f, 0.0f, -10.0f);

        glEnable(GL_LIGHTING);

        glEnable(GL_LIGHT0);

        GLfloat light_position[] = {0.2, 1.0, 10.0, 1.0};

        loadObj("/sdcard/tea.obj");

        return 1;

    }

};


int main(int argc, char *argv[]) {

    SDL_DisplayMode mode;

    state = SDLTest_CommonCreateState(argv, SDL_INIT_EVERYTHING);

    SDLTest_CommonInit(state);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);

    bool sdlmainloop = true;

    bool running = true;

    while (running) {

        SDL_Event event;

        while (SDL_PollEvent(&event)) {

            if (event.type == SDL_QUIT) {

                running = false;

            }

            if (event.type == SDL_MOUSEMOTION) {

                mouse.x = event.motion.x;

                mouse.y = event.motion.y;

            }

            SDL_GL_CreateContext(*state->windo

ws);

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            obj gg;

            gg.Render();

            SDL_GL_SwapWindow(*state->windows);

        }

    }

    return 0;

}

Asembly turial

 #include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_LINE_LENGTH 256

#define MAX_LINES 1000

/*95% pierwszego etapu  */


#define MAX_FILE_SIZE 1024 

char *run(char*cmd) {

char *echo;

    FILE *fp = popen(cmd, "r");

    if (fp == NULL) {

        perror("popen");

        return NULL;

    }

 size_t len = fread(echo, 1, MAX_FILE_SIZE, fp);  

 

      echo[len] = '\0';

   printf("%s",echo);

      pclose(fp);

      return echo;

}


char* name(char *old_name, const char *new_extension) {

    char *dot = strrchr(old_name, '.');

    if (dot != NULL) {

        strcpy(dot, new_extension);

    } else {

        strcat(old_name, new_extension);

    }

    return old_name;

}


void process_asm_file(const char *input_file, const char *output_file) {

    FILE *in_file = fopen(input_file, "r");

    FILE *out_file = fopen(output_file, "w");


    if (in_file == NULL || out_file == NULL) {

        printf("Nie można otworzyć pliku.\n");

        return;

    }


    char lines[MAX_LINES][MAX_LINE_LENGTH];

    int line_count = 0;


    // Wczytanie wszystkich linii do tablicy

    while (fgets(lines[line_count], MAX_LINE_LENGTH, in_file)) {

        // Sprawdzenie długości linii

        size_t len = strlen(lines[line_count]);


        // Usuwanie nowej linii na końcu, jeśli istnieje

        if (len > 0 && lines[line_count][len - 1] == '\n') {

            lines[line_count][len - 1] = '\0';

        }


        // Dodanie tabulatora przed .string, jeśli nie ma

        /*dodaje tab na poczatek lini a powinno po .string przed \" 

        cos innego puts?*/

        if (strstr(lines[line_count], ".string")) {

            char temp[MAX_LINE_LENGTH];

            snprintf(temp, MAX_LINE_LENGTH, "\t%s", lines[line_count]);

            strcpy(lines[line_count], temp);

        }


        line_count++;

    }

        if (line_count > 1) {

        strcpy(lines[1], "// Usunięto oryginalną linię");

    }

    // Pomiń zapisanie ostatnich dwóch linii

    if (line_count > 2) {

        line_count -= 2;

    }

    // Zapisanie zmodyfikowanych linii do nowego pliku

    for (int i = 0; i < line_count; i++) {

        fprintf(out_file, "%s\n", lines[i]);

    }


    fclose(in_file);

    fclose(out_file);

}


void zamien_znaki_w_miejscu(const char *plik) {

    FILE *fp = fopen(plik, "r+");

    if (!fp) {

        perror("Błąd przy otwieraniu pliku");

        return;

    }


    int c;

    while ((c = fgetc(fp)) != EOF) {

        if (c == '"') {

        //dwa znaki dodajemy ucina text 

            fseek(fp, -2, SEEK_CUR); // Cofnij wskaźnik o 1 bajt

            fputc('\\', fp); // Zastąp " na \"

            fputc('"', fp);

        }

    }


    fclose(fp);

}


int main(int argc, char *argv[]) {


    char viki[2024];

    char black[829];

    char cleo[2024];

    char youc[30];

char end[8]="\n\");";

    if (argc < 2) {

        printf("Podaj nazwę .c pliku?\n");

        printf("You.C\n");

        fgets(youc, sizeof(youc), stdin); // Use fgets for safer input handling

    } else {

        strcpy(youc, argv[1]); // Use the command-line argument if provided

    }

char len[40];

char*help;

sprintf(len,"/data/user/0/com.n0n3m4.droidc/files/gcc/bin/aarch64-linux-android-gcc -S %s\n",argv[1]);

run(len);

help=name(argv[1],".s");


strcpy(viki, help);


    // Remove trailing newline from filename

    

    viki[strcspn(viki, "\n")] = '\0'; 

    // Correctly remove newline

    

zamien_znaki_w_miejscu(viki); 

    FILE *infile = fopen(viki, "r");

    if (!infile) {

        fprintf(stderr, "Error: Could not open file: %s\n", viki);

        return 1;

    }


    FILE *outfile = fopen("returnn.c", "w");

    if (!outfile) {

        fprintf(stderr, "Error: Could not create output file.\n");

        fclose(infile);

        return 1;

    }


    while (fgets(black, sizeof(black), infile) != NULL) {

        black[strcspn(black, "\n")] = '\0'; // 

  sprintf(cleo,"asm(\"%s\\n\");\n", black);

        printf("%s", cleo);

       fputs(cleo, outfile);

    }


    fclose(infile);

    fclose(outfile);

    process_asm_file("returnn.c", "returnn.cc");

    // Wywołaj funkcję do zamiany znaków

printf("*******************");

    printf("RETURN.CC");

    printf("*******************");

    return 0;

}

/*

asm(" .arch armv8-a\n");

// Usunięto oryginalną linię

asm(" .text\n");

asm(" .section .rodata\n");

asm(" .align 3\n");

asm(".LC0:\n");

///====THIS=====

asm(" .string \"hell\"\n"); //space add

asm(" .text\n");

asm(" .align 2\n");

asm(" .global main\n");

asm(" .type main, %function\n");

asm("main:\n");

asm(".LFB0:\n");

asm(" .cfi_startproc\n");

asm(" stp x29, x30, [sp, -16]!\n");

asm(" .cfi_def_cfa_offset 16\n");

asm(" .cfi_offset 29, -16\n");

asm(" .cfi_offset 30, -8\n");

asm(" mov x29, sp\n");

asm(" adrp x0, .LC0\n");

asm(" add x0, x0, :lo12:.LC0\n");

asm(" bl printf\n");

asm(" mov w0, 0\n");

asm(" ldp x29, x30, [sp], 16\n");

asm(" .cfi_restore 30\n");

asm(" .cfi_restore 29\n");

asm(" .cfi_def_cfa_offset 0\n");

asm(" ret\n");

asm(" .cfi_endproc\n");

asm(".LFE0:\n");

asm(" .size main, .-main\n");


 /*


gcc -c -fPIE nmap.c -o nmap.o


gcc -c -fPIE nmap.c -o nmap.o


gcc nmap.o -o nmap -pie


chmod +x nmap


./nmap


*/


Thursday 29 August 2024

C4droid SDL2 rubixcube.obj


 

 #include <iostream>

#include <vector>

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <math.h>

#include "SDL2/SDL.h"

#include "SDL_test_common.h"

#if defined(__IPHONEOS__) || defined(__ANDROID__)

#define HAVE_OPENGLES

#endif

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "SDL_opengles.h"

struct mouse_handle

{

int x = 1;

int y = 1;

} mouse;

// Struktura do przechowywania wierzchołków

int gl;

struct Vertex

{

float x, y, z;

};

    static GLubyte color[8][4] = { {255, 0, 0, 0},};

// Globalne zmienne przechowujące aktualnie rysowane prymitywy

//PrimitiveType currentPrimitiveType;

std::vector < Vertex > vertices;


// Uproszczona funkcja glBegin tutaj zamiast enum misialem uzyc int gl bo juz jest zdwfiniowane w sdl_opengl.h jako int i nie chcialo puscic 

void glBegin(int gl)

{

//currentPrimitiveType = primitiveType;

vertices.clear(); // Wyczyść bufor wierzchołków

}


// Uproszczona funkcja glVertex3f

void glVertex3f(float x, float y, float z)

{

vertices.push_back({x, y, z});

}


// Uproszczona funkcja glEnd

void glEnd() {

    switch (gl) {

        case GL_POINTS:

            for (const auto &vertex : vertices) {

                std::cout << "Point: (" << vertex.x << ", " << vertex.y << ", " << vertex.z << ")" << std::endl;

            }

            glEnableClientState(GL_COLOR_ARRAY);

            glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);

            glEnableClientState(GL_VERTEX_ARRAY);

            glVertexPointer(3, GL_FLOAT, sizeof(Vertex), (void*)vertices.data());

            glDrawArrays(GL_POINTS, 0, vertices.size());

            break;


        case GL_TRIANGLES:

            if (vertices.size() % 3 != 0) {

                std::cerr << "Warning: Number of vertices is not a multiple of 3 for GL_TRIANGLES" << std::endl;

            }

            for (size_t i = 0; i < vertices.size(); i += 3) {

                std::cout << "Triangle: (" << vertices[i].x << ", " << vertices[i].y << ", " << vertices[i].z << ") -> ("

                          << vertices[i + 1].x << ", " << vertices[i + 1].y << ", " << vertices[i + 1].z << ") -> ("

                          << vertices[i + 2].x << ", " << vertices[i + 2].y << ", " << vertices[i + 2].z << ")" << std::endl;

            }

            glEnableClientState(GL_COLOR_ARRAY);

            glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);

            glEnableClientState(GL_VERTEX_ARRAY);

            glVertexPointer(3, GL_FLOAT, sizeof(Vertex), (void*)vertices.data());

            glDrawArrays(GL_TRIANGLES, 0, vertices.size());

            break;


        case GL_LINES:

            if (vertices.size() % 2 != 0) {

                std::cerr << "Warning: Number of vertices is not a multiple of 2 for GL_LINES" << std::endl;

            }

            for (size_t i = 0; i < vertices.size(); i += 2) {

                std::cout << "Line: (" << vertices[i].x << ", " << vertices[i].y << ", " << vertices[i].z << ") -> ("

                          << vertices[i + 1].x << ", " << vertices[i + 1].y << ", " << vertices[i + 1].z << ")" << std::endl;

            }

            glEnableClientState(GL_COLOR_ARRAY);

            glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);

            glEnableClientState(GL_VERTEX_ARRAY);

            glVertexPointer(3, GL_FLOAT, sizeof(Vertex), (void*)vertices.data());

            glDrawArrays(GL_LINES, 0, vertices.size());

            break;


        default:

            std::cerr << "Unsupported primitive type!" << std::endl;

            break;

    }

}



GLuint car;

float carrot;

void loadObj(char *fname)

{

   FILE *fp;

   int read;

   GLfloat x, y, z;

   char ch;


   fp=fopen(fname,"r");

   if (!fp)

  {

    printf("can't open file %s\n", fname);

    exit(1);

  }

   glPointSize(2.0);


    glPushMatrix();

         glRotatef(mouse.x % 360, mouse.y % 360, mouse.x % 360, mouse.y % 360);

  //  glTranslatef(0.0,0.0,-1);

    

glPointSize(10.0f); 

    glBegin(GL_TRIANGLES);

    while(!(feof(fp)))

    {

     read=fscanf(fp,"%c %f %f %f",&ch,&x,&y,&z);

     if(read==4&&ch=='v')

    {

     glVertex3f(x,y,z);

     }

   }

   glEnd();

   

   glPopMatrix();

 

   fclose(fp);

}

void setPerspective(float fov, float aspect, float znear, float zfar) {

    float ymax = znear * tanf(fov * M_PI / 360.0f);

    float ymin = -ymax;

    float xmin = ymin * aspect;

    float xmax = ymax * aspect;


    glFrustumf(xmin, xmax, ymin, ymax, znear, zfar);

}



/* this */

SDLTest_CommonState *state;

SDL_Event *event;

static SDL_GLContext *context;

struct obj

{

float x;

float y;

float z;

int Render()

{

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();


    // Ustawienie perspektywy (fov, aspect ratio, near, far)

    setPerspective(45.0f, 1.0f, 0.1f, 100.0f);


    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();


    // Przesunięcie kamery do tyłu, aby oddalić obiekt

    glTranslatef(0.0f, 0.0f, -5.0f);


    glEnable(GL_LIGHTING);

    glEnable(GL_LIGHT0);


    GLfloat light_position[] = { 0.2, 1.0, 10.0, 1.0 };


    loadObj("/sdcard/RubixCube.obj");


    return 1;

}


};




int main(int argc, char *argv[])

{

SDL_DisplayMode mode;

state = SDLTest_CommonCreateState(argv, SDL_INIT_EVERYTHING);

SDLTest_CommonInit(state);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);

bool sdlmainloop = true;

bool running = true;


while (running)

{

SDL_Event event;

while (SDL_PollEvent(&event))

{

if (event.type == SDL_QUIT)

{

running = false;

}

if (event.type == SDL_MOUSEMOTION)

{

mouse.x = event.motion.x;

mouse.y = event.motion.y;

}

SDL_GL_CreateContext(*state->windows);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/* draw() */

obj gg;

gg.Render();

SDL_GL_SwapWindow(*state->windows);

}

}

return 0;

}


Wednesday 28 August 2024

glBegin world's on sdl2 c4

 #include <iostream>

#include <vector>

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <math.h>

#include "SDL2/SDL.h"

#include "SDL_test_common.h"

#if defined(__IPHONEOS__) || defined(__ANDROID__)

#define HAVE_OPENGLES

#endif

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "SDL_opengles.h"

// Struktura do przechowywania wierzchołków

int gl;

struct Vertex

{

 float x, y, z;

};

    static GLubyte color[8][4] = { {255, 0, 0, 0},};

// Globalne zmienne przechowujące aktualnie rysowane prymitywy

//PrimitiveType currentPrimitiveType;

std::vector < Vertex > vertices;


// Uproszczona funkcja glBegin tutaj zamiast enum misialem uzyc int gl bo juz jest zdwfiniowane w sdl_opengl.h jako int i nie chcialo puscic 

void glBegin(int gl)

{

 //currentPrimitiveType = primitiveType;

 vertices.clear(); // Wyczyść bufor wierzchołków

}


// Uproszczona funkcja glVertex3f

void glVertex3f(float x, float y, float z)

{

 vertices.push_back({x, y, z});

}


// Uproszczona funkcja glEnd

void glEnd() {

    switch (gl) {

        case GL_POINTS:

            for (const auto &vertex : vertices) {

                std::cout << "Point: (" << vertex.x << ", " << vertex.y << ", " << vertex.z << ")" << std::endl;

            }

            glEnableClientState(GL_COLOR_ARRAY);

            glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);

            glEnableClientState(GL_VERTEX_ARRAY);

            glVertexPointer(3, GL_FLOAT, sizeof(Vertex), (void*)vertices.data());

            glDrawArrays(GL_POINTS, 0, vertices.size());

            break;


        case GL_TRIANGLES:

            if (vertices.size() % 3 != 0) {

                std::cerr << "Warning: Number of vertices is not a multiple of 3 for GL_TRIANGLES" << std::endl;

            }

            for (size_t i = 0; i < vertices.size(); i += 3) {

                std::cout << "Triangle: (" << vertices[i].x << ", " << vertices[i].y << ", " << vertices[i].z << ") -> ("

                          << vertices[i + 1].x << ", " << vertices[i + 1].y << ", " << vertices[i + 1].z << ") -> ("

                          << vertices[i + 2].x << ", " << vertices[i + 2].y << ", " << vertices[i + 2].z << ")" << std::endl;

            }

            glEnableClientState(GL_COLOR_ARRAY);

            glColorPointer(4, GL_UNSIGNED_BYTE, 0, color);

            glEnableClientState(GL_VERTEX_ARRAY);

            glVertexPointer(3, GL_FLOAT, sizeof(Vertex), (void*)vertices.data());

            glDrawArrays(GL_TRIANGLES, 0, vertices.size());

            break;


        default:

            std::cerr << "Unsupported primitive type!" << std::endl;

            break;

    }

}



struct mouse_handle

{

 int x = 1;

 int y = 1;

} mouse;

/* this */

SDLTest_CommonState *state;

SDL_Event *event;

static SDL_GLContext *context;

struct obj

{

 float x;

 float y;

 float z;

 int Render()

 {

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

//gluPerspective(45.0, (double)screen_width/screen_height, 1.0, 200.0);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

//glTranslatef(0.0f, 0.0f, -5.0f); // Odsunięcie kamery


 glEnable(GL_LIGHTING);

  glEnable(GL_LIGHT0);


  GLfloat light_position[] = { 0.2, 1.0, 10.0, 1.0 };

glPointSize(10.0f); // Ustawia rozmiar punktu na 10 pikseli


     glBegin(GL_POINTS);

    glVertex3f(0.4f, 0.0f, 0.0f);

 

   // glVertex3f(mouse.x, mouse.y, 0.0f);

    glVertex3f(0.0f, 0.5f, 0.0f);

    glVertex3f(1.0f, 0.5f, 1.0f);

    glVertex3f(0.0f, -0.5f, 0.0f);

    glEnd();



  return 1;

 }

};




int main(int argc, char *argv[])

{

 SDL_DisplayMode mode;

 state = SDLTest_CommonCreateState(argv, SDL_INIT_EVERYTHING);

 SDLTest_CommonInit(state);

 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);

 bool sdlmainloop = true;

 bool running = true;


 while (running)

 {

  SDL_Event event;

  while (SDL_PollEvent(&event))

  {

   if (event.type == SDL_QUIT)

   {

    running = false;

   }

   if (event.type == SDL_MOUSEMOTION)

   {

    mouse.x = event.motion.x;

    mouse.y = event.motion.y;

   }

   SDL_GL_CreateContext(*state->windows);

   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  

   /* 

draw() */

   obj gg;

   gg.x = 0.2f;

   gg.y = 0.4f;

   gg.z = 0.6f;

   gg.Render();

   SDL_GL_SwapWindow(*state->windows);

  }

 }

 return 0;

}

Sunday 25 August 2024

TCP C server console

 #include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

//#include "run.cpp"

#define PORT 8080

#define BUFFER_SIZE 1024

#include <stdio.h>

#define MAX_FILE_SIZE 1024 

char *run(char*cmd) {

char *echo;

    FILE *fp = popen(cmd, "r");

    if (fp == NULL) {

        perror("popen");

        return NULL;

    }

 size_t len = fread(echo, 1, MAX_FILE_SIZE, fp);  

 

      echo[len] = '\0';

   printf("%s",echo);

      pclose(fp);

      return echo;

}

struct dialog {

char *body;

}msg;

void handle_post(int client_socket, char *data) {

    printf("Received POST data: %s\n", data);

    const char *response =

        "HTTP/1.1 200 OK\r\n"

        "Content-Type: text/plain\r\n"

        "Content-Length: 24\r\n"

        "\r\n"

        "Hello, POST!";

    send(client_socket, response, strlen(response), 0);

}


void handle_get(int client_socket, const char *uri) {

    if (strcmp(uri, "/") == 0) {

        // Jeśli URI to "/", wyślij stronę HTML z formularzem

        FILE *fp = fopen("console.html", "r");

        if (fp == NULL) {

            perror("Failed to open console.html");

            close(client_socket);

            return;

        }


        // Odczytanie zawartości pliku

        fseek(fp, 0, SEEK_END);

        long file_size = ftell(fp);

        fseek(fp, 0, SEEK_SET);

        char *file_content = (char *)malloc(file_size + 1);

        fread(file_content, 1, file_size, fp);

        file_content[file_size] = '\0';

        fclose(fp);


        char response[BUFFER_SIZE];

        snprintf(response, sizeof(response),

                 "HTTP/1.1 200 OK\r\n"

                 "Content-Type: text/html\r\n"

                 "Content-Length: %ld\r\n"

                 "\r\n%s",

                 file_size, file_content);

        send(client_socket, response, strlen(response), 0);

        free(file_content);

    } 

     if (strcmp(uri, "/console") == 0) {

       

                handle_post(client_socket,    run(msg.body));

     }

    else {

        // Domyślna odpowiedź dla innych URI

        const char *response =

            "HTTP/1.1 404 Not Found\r\n"

            "Content-Type: text/plain\r\n"

            "Content-Length: 13\r\n"

            "\r\n"

            "404 Not Found";

        send(client_socket, response, strlen(response), 0);

    }

}



void parse_request_line(char *req, char *method, char *uri) {

    sscanf(req, "%s %s", method, uri);

}


void create_console_html(char*msg) {

    FILE *fp = fopen("console.html", "w");

    if (fp == NULL) {

        perror("Failed to create console.html");

        return;

    }

 // char*cmd=(char*)malloc(sizeof(run(msg)));

//  cmd=run(msg);

  

    fprintf(fp,

            "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>Simple Console</title></head><body><h1>Simple Server Console</h1><form method=\"POST\" action=\"/console\"><label for=\"command\">Enter command:</label><br><input type=\"text\" id=\"command\" name=\"command\" size=\"50\"><br><br><input type=\"submit\" value=\"Execute\"></form><h2>Output:</h2><pre id=\"output\">%s<!-- Output  the command will be displayed here --></pre></body></html>",msg);

    fclose(fp);

}


int main() {

    int server_socket, client_socket;

    struct sockaddr_in server_addr, client_addr;

    socklen_t client_addr_len = sizeof(client_addr);

    char buffer[BUFFER_SIZE];



printf("https://127.0.0.1:8080");

    // Create socket

    server_socket = socket(AF_INET, SOCK_STREAM, 0);

    if (server_socket < 0) {

        perror("Socket creation failed");

        exit(EXIT_FAILURE);

    }


    // Bind the socket to the specified port

    server_addr.sin_family = AF_INET;

    server_addr.sin_addr.s_addr = INADDR_ANY;

    server_addr.sin_port = htons(PORT);

    if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {

        perror("Bind failed");

        close(server_socket);

        exit(EXIT_FAILURE);

    }


    // Listen for incoming connections

    if (listen(server_socket, 10) < 0) {

        perror("Listen failed");

        close(server_socket);

        exit(EXIT_FAILURE);

    }


    // Inform the user how to access the server

//    printf("Server is listening on https://127.0.0.1:%d...\n", PORT);

    // Create the HTML console template

    create_console_html("");

    // Main loop: accept and handle requests

    while (1) {

        client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);

        if (client_socket < 0) {

            perror("Accept failed");

            continue;

        }


        int received = recv(client_socket, buffer, sizeof(buffer) - 1, 0);

        if (received < 0) {

            perror("recv failed");

            close(client_socket);

            continue;

        }

        buffer[received] = '\0'; // Null-terminate the received data


        char method[20], uri[1024];

        parse_request_line(buffer, method, uri);


        if (strcmp(method, "GET") == 0) {

            handle_get(client_socket, uri);

        } else if (strcmp(method, "POST") == 0) {

            msg.body = strstr(buffer, "\r\n\r\n");

            if (msg.body) {

                msg.body += 12; // Move pointer past the "\r\n\r\n"

            }

            char response[BUFFER_SIZE];

        snprintf(response, sizeof(response)+sizeof(run(msg.body)),

                 "HTTP/1.1 200 OK\r\n"

                 "Content-Type: text/plain\r\n"                     "\r\n%s", run(msg.body));

                 create_console_html(response);

                 send(client_socket, response, strlen(response), 0);

        }


        close(client_socket);

    }


    close(server_socket);

    return 0;

}


Console SDL2 .ttf requaied to run this projekt

 

 rubik-black.font font.ttf

Monday 19 August 2024

C4droid SDL2 & openGl load big .obj

 /******ten kod dziala ale nie chciał bym żeby tak wyglądał mój kot**************/

/*************21:31********************/

/**************pon*********************/

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <math.h>

#include "SDL2/SDL.h"

#include "SDL_test_common.h"

#if defined(__IPHONEOS__) || defined(__ANDROID__)

#define HAVE_OPENGLES

#endif

#include "SDL_opengles.h"

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// brak #include <glad/glad.h>

#define MAX_VERTICES 1000

#define MAX_INDICES 3000

struct Vertex {

    GLfloat x, y, z;  // Współrzędne wierzchołka

    GLubyte r, g, b, a;  // Kolor

    GLfloat nx, ny, nz;  // Normalne

};

struct Vertex* vertices = NULL;

int* indices = NULL;  // Zakładamy, że indeksy są całkowite

 int vertex_count = 0;

 int index_count = 0;

 int vertex_capacity = 1000;

int index_capacity = 1000;


void parseOBJ(const char* filename) {

    FILE* file = fopen(filename, "r");

    if (!file) {

        printf("Nie można otworzyć pliku: %s\n", filename);

        return;

    }


    vertices = (struct Vertex*)malloc(vertex_capacity * sizeof(struct Vertex));

    indices = (int*)malloc(index_capacity * sizeof(int));


    char line[128];

    while (fgets(line, sizeof(line), file)) {

        if (strncmp(line, "v ", 2) == 0) {

            if (vertex_count >= vertex_capacity) {

                vertex_capacity *= 2;

                vertices = (struct Vertex*)realloc(vertices, vertex_capacity * sizeof(struct Vertex));

            }

            sscanf(line, "v %f %f %f", 

                   &vertices[vertex_count].x, 

                   &vertices[vertex_count].y, 

                   &vertices[vertex_count].z);

            // Ustaw losowe kolory (lub inne dane)

            vertices[vertex_count].r = 150;

            vertices[vertex_count].g = 10;

            vertices[vertex_count].b = 0;

            vertices[vertex_count].a = 25;


            vertex_count++;

        } else if (strncmp(line, "vn ", 3) == 0) {

            if (vertex_count > 0) {

                sscanf(line, "vn %f %f %f", 

                       &vertices[vertex_count - 1].nx, 

                       &vertices[vertex_count - 1].ny, 

                       &vertices[vertex_count - 1].nz);

            }

        } else if (strncmp(line, "f ", 2) == 0) {

            if (index_count >= index_capacity) {

                index_capacity *= 2;

                indices = (int*)realloc(indices, index_capacity * sizeof(int));

            }

            /*tutaj zazwyczaj jest blad bo występują rózne formaty .obj 

            if (sscanf(line, "f %d//%d %d//%d %d//%d", &v1, &vn1, &v2, &vn2, &v3, &vn3) == 6) {

    // Obsłuż format v//vn

} else if (sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d", &v1, &vt1, &vn1, &v2, &vt2, &vn2, &v3, &vt3, &vn3) == 9) {

    // Obsłuż format v/vt/vn

} else {

    // Obsłuż prostsze przypadki

}

            */

            int v1, v2, v3;

            sscanf(line, "f %d//%d//%d", &v1, &v2, &v3);

            indices[index_count++] = v1;

            indices[index_count++] = v2;

            indices[index_count++] = v3;

            index_count++;

            v1--;

            v2--;

            v3--;

        }

    }


    fclose(file);

}


/*tutaj chciałem użyć vao i vbo ale nic z tego 

GLuint VAO, VBO;


void createVAO() {

    glGenVertexArraysOES(1, &VAO);

    glBindVertexArrayOES(VAO);


    glGenBuffers(1, &VBO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);


    // Konfiguracja atrybutu pozycji

  //  glVertexPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);

  

glVertexPointer(3, GL_FLOAT, sizeof(&vertices), vertices);

    //glEnableVertexAttribArray(0);


    glBindVertexArrayOES(0);

}

*/

struct mouse_handle

{

int x = 1;

int y = 1;

} mouse;

/*this openGL in SDL*/

SDLTest_CommonState *state;

SDL_Event *event;

static SDL_GLContext *context;

struct obj {

    float x;

    float y;

    float z;

void renderCube() {

 //   glPushMatrix();

    glMatrixMode(GL_PROJECTION);

glLoadIdentity();

    glRotatef(mouse.x % 360, mouse.y % 360, mouse.x % 360, mouse.y % 360);

glOrthox(-80.0, -80.0, -80.0, -80.0, -80.0, -80.0);


glMatrixMode(GL_MODELVIEW);


glTranslatef(0.0f, 0.0f, -1.0f); 



    glEnable(GL_LIGHTING);

    glEnable(GL_LIGHT0);

    

    GLfloat light_position[] = { 0.0, 0.0, 10.0, 1.0 };

    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    

    GLfloat ambient_light[] = { 0.3, 0.2, 0.2, 0.0 };

    GLfloat diffuse_light[] = { 0.5, 0.2, 0.1, 1.0 };

    GLfloat specular_light[] = { 1.0, 0.3, 1.0, 1.0 };

    

    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_light);

    glEnableClientState(GL_COLOR_ARRAY);

   glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(struct Vertex), &vertices[0].r);


    glEnableClientState(GL_VERTEX_ARRAY);

    glVertexPointer(3, GL_FLOAT, sizeof(struct Vertex), &vertices[0].x);


    glEnableClientState(GL_NORMAL_ARRAY);

    glNormalPointer(GL_FLOAT, sizeof(struct Vertex), &vertices[0].nx);


    glDrawElements(GL_TRIANGLES, index_count*3, GL_UNSIGNED_INT, indices);


    glDisableClientState(GL_VERTEX_ARRAY);

    glDisableClientState(GL_NORMAL_ARRAY);

    glDisableClientState(GL_COLOR_ARRAY);

glLoadIdentity();

//    glPopMatrix();

}

};

int main(int argc, char *argv[])

{

    parseOBJ("cat.obj");

  //createVAO();

SDL_DisplayMode mode;

//SDL_GL_MakeCurrent(win, context);

state = SDLTest_CommonCreateState(argv, SDL_INIT_EVERYTHING);

SDLTest_CommonInit(state); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 0);

bool sdlmainloop = true;

bool running = true;

//createVAO();

while (running)

{

SDL_Event event;

while (SDL_PollEvent(&event))

{

if (event.type == SDL_QUIT)

{

running = false;

}

if (event.type == SDL_MOUSEMOTION)

{

mouse.x = event.motion.x;

mouse.y = event.motion.y;

}

SDL_GL_CreateContext(*state->windows);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

   //createVAO();

/*draw()*/

/*

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrthox(-30.0, -30.0, -30.0, -30.0, -30.0, -30.0);


glMatrixMode(GL_MODELVIEW);


glTranslatef(0.0f, 0.0f, -1.0f);  // Oddalenie widoku

*/

obj gg;

gg.x = 0.2f;

gg.y = 0.4f;

gg.z = 0.6f;

gg.renderCube();


SDL_GL_SwapWindow(*state->windows);

}

}

free(vertices);

free(indices);


return 0;

}