SDL2'ye uyarlanmış kodda kare atlama

The User 0

Hectopat
Katılım
14 Eylül 2020
Mesajlar
2.117
Çözümler
42
Daha fazla  
Sistem Özellikleri
HP Laptop 15-da1083nt
Intel Core i5-8265U @ 1.60GHz
Intel UHD Graphics 620
NVidia GeForce MX130
WD NVMe 480GB
A-Data 8GB CL17 2400MHz
HP 22es 1080p 60Hz
HyperX PulseFire Raid
Cinsiyet
Erkek
Meslek
Öğrenci
Önceden Arduino'da kullandığım GitHub - Avamander/arduino-tvout: Arduino-TVout kütüphanesinde 3 boyutlu olarak küp çıktısı veren bir parça kod vardı. Ben de bunu C# kullanarak SDL2'ye uyarlamayı denedim. Çıktı geliyor ama şekil tam anlaşılır değil. Arada bazı kareleri atlıyor gibi. Nasıl bir düzenleme yapmalıyım?

Benim kullandığım kod:
C#:
// Öncesinde bununla alakalı kod yok. new CubeRender(); satırı çalışıyor ve buraya geliyor.
class CubeRender
{
    bool isRunning = false;
    IntPtr GameWindow, Renderer;


    int zOff = 150;
    int xOff = 0;
    int yOff = 0;
    int cSize = 50;
    int view_plane = 64;
    float angle = (float)(PI / 60);

    float[][] cube3d;
    char[][] cube2d;


    public CubeRender()
    {
        Init();
        isRunning = true;
        while (isRunning)
        {
            Update();
        }
        Destroy();
    }
    void Init()
    {
        SDL_Init(SDL_INIT_VIDEO); // Init video
        IMG_Init(IMG_InitFlags.IMG_INIT_PNG); // Init image library

        GameWindow = SDL_CreateWindow("SDL Tutorial", 50, 50, 800, 600, SDL_WindowFlags.SDL_WINDOW_SHOWN); // Create window
        Renderer = SDL_CreateRenderer(GameWindow, 0, SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC); // Create window rendering

        cube3d = new float[][]{
            new float[] { xOff - cSize, yOff + cSize, zOff - cSize },
            new float[] { xOff + cSize, yOff + cSize, zOff - cSize },
            new float[] { xOff - cSize, yOff - cSize, zOff - cSize },
            new float[] { xOff + cSize, yOff - cSize, zOff - cSize },
            new float[] { xOff - cSize, yOff + cSize, zOff + cSize },
            new float[] { xOff + cSize, yOff + cSize, zOff + cSize },
            new float[] { xOff - cSize, yOff - cSize, zOff + cSize },
            new float[] { xOff + cSize, yOff - cSize, zOff + cSize }
        };

        cube2d = new char[][]
        {
            new char[]{ ' ', ' ' },
            new char[]{ ' ', ' ' },
            new char[]{ ' ', ' ' },
            new char[]{ ' ', ' ' },
            new char[]{ ' ', ' ' },
            new char[]{ ' ', ' ' },
            new char[]{ ' ', ' ' },
            new char[]{ ' ', ' ' }
        };
    }

    void Update()
    {
        while (SDL_PollEvent(out SDL.SDL_Event e) == 1) // If any event
        {
            switch (e.type)
            {
                case SDL_EventType.SDL_QUIT: // Alt+F4 or close window
                    isRunning = false;
                    break;
            }
        }

        SDL_SetRenderDrawColor(Renderer, 135, 206, 235, 255); // Set clearing color
        SDL_RenderClear(Renderer); // Clear with current color

        int rsteps = new Random().Next(10, 60);
        switch (new Random().Next(6))
        {
            case 0:
                for (int i = 0; i < rsteps; i++)
                {
                    zrotate(angle);
                    printcube();
                }
                break;
            case 1:
                for (int i = 0; i < rsteps; i++)
                {
                    zrotate((float)(2 * PI - angle));
                    printcube();
                }
                break;
            case 2:
                for (int i = 0; i < rsteps; i++)
                {
                    xrotate(angle);
                    printcube();
                }
                break;
            case 3:
                for (int i = 0; i < rsteps; i++)
                {
                    xrotate((float)(2 * PI - angle));
                    printcube();
                }
                break;
            case 4:
                for (int i = 0; i < rsteps; i++)
                {
                    yrotate(angle);
                    printcube();
                }
                break;
            case 5:
                for (int i = 0; i < rsteps; i++)
                {
                    yrotate((float)(2 * PI - angle));
                    printcube();
                }
                break;
        }

        SDL_RenderPresent(Renderer); // Draw current

        SDL_Delay(100); // Wait 10 ms
    }

    void Destroy()
    {
        SDL_DestroyRenderer(Renderer);
        SDL_DestroyWindow(GameWindow);
        SDL_Quit();
    }

    void printcube()
    {
        //calculate 2d points
        for (byte i = 0; i < 8; i++)
        {
            cube2d[i][0] = (char)((cube3d[i][0] * view_plane / cube3d[i][2]) + (600 / 2));
            cube2d[i][1] = (char)((cube3d[i][1] * view_plane / cube3d[i][2]) + (800 / 2));
        }
        draw_cube();
    }

    void zrotate(float q)
    {
        float tx, ty, temp;
        for (byte i = 0; i < 8; i++)
        {
            tx = cube3d[i][0] - xOff;
            ty = cube3d[i][1] - yOff;
            temp = (float)(tx * Cos(q) - ty * Sin(q));
            ty = (float)(tx * Sin(q) + ty * Cos(q));
            tx = temp;
            cube3d[i][0] = tx + xOff;
            cube3d[i][1] = ty + yOff;
        }
    }

    void yrotate(float q)
    {
        float tx, tz, temp;
        for (byte i = 0; i < 8; i++)
        {
            tx = cube3d[i][0] - xOff;
            tz = cube3d[i][2] - zOff;
            temp = (float)(tz * Cos(q) - tx * Sin(q));
            tx = (float)(tz * Sin(q) + tx * Cos(q));
            tz = temp;
            cube3d[i][0] = tx + xOff;
            cube3d[i][2] = tz + zOff;
        }
    }

    void xrotate(float q)
    {
        float ty, tz, temp;
        for (byte i = 0; i < 8; i++)
        {
            ty = cube3d[i][1] - yOff;
            tz = cube3d[i][2] - zOff;
            temp = (float)(ty * Cos(q) - tz * Sin(q));
            tz = (float)(ty * Sin(q) + tz * Cos(q));
            ty = temp;
            cube3d[i][1] = ty + yOff;
            cube3d[i][2] = tz + zOff;
        }
    }

    void draw_cube()
    {
        SDL_SetRenderDrawColor(Renderer, 255, 255, 255, 255);

        SDL_RenderDrawLine(Renderer, cube2d[0][0], cube2d[0][1], cube2d[1][0], cube2d[1][1]);
        SDL_RenderDrawLine(Renderer, cube2d[0][0], cube2d[0][1], cube2d[2][0], cube2d[2][1]);
        SDL_RenderDrawLine(Renderer, cube2d[0][0], cube2d[0][1], cube2d[4][0], cube2d[4][1]);
        SDL_RenderDrawLine(Renderer, cube2d[1][0], cube2d[1][1], cube2d[5][0], cube2d[5][1]);
        SDL_RenderDrawLine(Renderer, cube2d[1][0], cube2d[1][1], cube2d[3][0], cube2d[3][1]);
        SDL_RenderDrawLine(Renderer, cube2d[2][0], cube2d[2][1], cube2d[6][0], cube2d[6][1]);
        SDL_RenderDrawLine(Renderer, cube2d[2][0], cube2d[2][1], cube2d[3][0], cube2d[3][1]);
        SDL_RenderDrawLine(Renderer, cube2d[4][0], cube2d[4][1], cube2d[6][0], cube2d[6][1]);
        SDL_RenderDrawLine(Renderer, cube2d[4][0], cube2d[4][1], cube2d[5][0], cube2d[5][1]);
        SDL_RenderDrawLine(Renderer, cube2d[7][0], cube2d[7][1], cube2d[6][0], cube2d[6][1]);
        SDL_RenderDrawLine(Renderer, cube2d[7][0], cube2d[7][1], cube2d[3][0], cube2d[3][1]);
        SDL_RenderDrawLine(Renderer, cube2d[7][0], cube2d[7][1], cube2d[5][0], cube2d[5][1]);
    }

}

Orijinal hali:
C++:
// https://github.com/Avamander/arduino-tvout/blob/master/examples/DemoNTSC/DemoNTSC.pde

#include <TVout.h>

TVout TV;

int zOff = 150;
int xOff = 0;
int yOff = 0;
int cSize = 50;
int view_plane = 64;
float angle = PI/60;

float cube3d[8][3] = {
  {xOff - cSize,yOff + cSize,zOff - cSize},
  {xOff + cSize,yOff + cSize,zOff - cSize},
  {xOff - cSize,yOff - cSize,zOff - cSize},
  {xOff + cSize,yOff - cSize,zOff - cSize},
  {xOff - cSize,yOff + cSize,zOff + cSize},
  {xOff + cSize,yOff + cSize,zOff + cSize},
  {xOff - cSize,yOff - cSize,zOff + cSize},
  {xOff + cSize,yOff - cSize,zOff + cSize}
};
unsigned char cube2d[8][2];


void setup() {
  TV.begin(NTSC,120,96);
  randomSeed(analogRead(0));
}

void loop() {
  int rsteps = random(10,60);
  switch(random(6)) {
    case 0:
      for (int i = 0; i < rsteps; i++) {
        zrotate(angle);
        printcube();
      }
      break;
    case 1:
      for (int i = 0; i < rsteps; i++) {
        zrotate(2*PI - angle);
        printcube();
      }
      break;
    case 2:
      for (int i = 0; i < rsteps; i++) {
        xrotate(angle);
        printcube();
      }
      break;
    case 3:
      for (int i = 0; i < rsteps; i++) {
        xrotate(2*PI - angle);
        printcube();
      }
      break;
    case 4:
      for (int i = 0; i < rsteps; i++) {
        yrotate(angle);
        printcube();
      }
      break;
    case 5:
      for (int i = 0; i < rsteps; i++) {
        yrotate(2*PI - angle);
        printcube();
      }
      break;
  }
}

void printcube() {
  //calculate 2d points
  for(byte i = 0; i < 8; i++) {
    cube2d[i][0] = (unsigned char)((cube3d[i][0] * view_plane / cube3d[i][2]) + (TV.hres()/2));
    cube2d[i][1] = (unsigned char)((cube3d[i][1] * view_plane / cube3d[i][2]) + (TV.vres()/2));
  }
  TV.delay_frame(1);
  TV.clear_screen();
  draw_cube();
}

void zrotate(float q) {
  float tx,ty,temp;
  for(byte i = 0; i < 8; i++) {
    tx = cube3d[i][0] - xOff;
    ty = cube3d[i][1] - yOff;
    temp = tx * cos(q) - ty * sin(q);
    ty = tx * sin(q) + ty * cos(q);
    tx = temp;
    cube3d[i][0] = tx + xOff;
    cube3d[i][1] = ty + yOff;
  }
}

void yrotate(float q) {
  float tx,tz,temp;
  for(byte i = 0; i < 8; i++) {
    tx = cube3d[i][0] - xOff;
    tz = cube3d[i][2] - zOff;
    temp = tz * cos(q) - tx * sin(q);
    tx = tz * sin(q) + tx * cos(q);
    tz = temp;
    cube3d[i][0] = tx + xOff;
    cube3d[i][2] = tz + zOff;
  }
}

void xrotate(float q) {
  float ty,tz,temp;
  for(byte i = 0; i < 8; i++) {
    ty = cube3d[i][1] - yOff;
    tz = cube3d[i][2] - zOff;
    temp = ty * cos(q) - tz * sin(q);
    tz = ty * sin(q) + tz * cos(q);
    ty = temp;
    cube3d[i][1] = ty + yOff;
    cube3d[i][2] = tz + zOff;
  }
}

void draw_cube() {
  TV.draw_line(cube2d[0][0],cube2d[0][1],cube2d[1][0],cube2d[1][1],WHITE);
  TV.draw_line(cube2d[0][0],cube2d[0][1],cube2d[2][0],cube2d[2][1],WHITE);
  TV.draw_line(cube2d[0][0],cube2d[0][1],cube2d[4][0],cube2d[4][1],WHITE);
  TV.draw_line(cube2d[1][0],cube2d[1][1],cube2d[5][0],cube2d[5][1],WHITE);
  TV.draw_line(cube2d[1][0],cube2d[1][1],cube2d[3][0],cube2d[3][1],WHITE);
  TV.draw_line(cube2d[2][0],cube2d[2][1],cube2d[6][0],cube2d[6][1],WHITE);
  TV.draw_line(cube2d[2][0],cube2d[2][1],cube2d[3][0],cube2d[3][1],WHITE);
  TV.draw_line(cube2d[4][0],cube2d[4][1],cube2d[6][0],cube2d[6][1],WHITE);
  TV.draw_line(cube2d[4][0],cube2d[4][1],cube2d[5][0],cube2d[5][1],WHITE);
  TV.draw_line(cube2d[7][0],cube2d[7][1],cube2d[6][0],cube2d[6][1],WHITE);
  TV.draw_line(cube2d[7][0],cube2d[7][1],cube2d[3][0],cube2d[3][1],WHITE);
  TV.draw_line(cube2d[7][0],cube2d[7][1],cube2d[5][0],cube2d[5][1],WHITE);
}
 
Son düzenleme:

Yeni konular

Geri
Yukarı