c++ - 如何与opengl并行运行程序

标签 c++ opengl parallel-processing glut

<分区>

我想运行 opengl 并且我还希望我的 C 代码被执行... xD 创建窗口后没有代码执行...这正常吗? 或者我只需要更好地理解 opengl :D

也许我需要将我的 C 代码放入某个 opengl 循环 xD 中,比如放入空闲函数中^^

//============================================================================
// Name        : cpu_Emulation_19-10-13.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
//https://fms.komkon.org/EMUL8/HOWTO.html

// todo: iam programming the dissambler which just checks my given in commands.. 1927 - 31.10.2019
#include <iostream>
#include <stdint.h>
#include "C8Klasse.h"
#include "Cartridge.h"
#include <stdio.h>
#include <GL/glut.h>
#include <intrin.h> // damit kann man zyklen auslesen...https://stackoverflow.com/questions/13772567/how-to-get-the-cpu-cycle-count-in-x86-64-from-c
using namespace std;
//  Windows
//#ifdef _WIN32

// what does this whole thing: it ist a Chip8 Interpreter
unsigned short int memory[1000]; // annahem
void initMemory() {
    memory[0] = 0;
    memory[1] = 1;
}
void display() {

}
void glutInitRoutines() {
    glutInitWindowSize(64, 32);
    glutCreateWindow("Chip8 V5");
    glutDisplayFunc(display); // die obige Funktion display wird als Bildchirm behandler bekanntgemacht
    //glutSpecialFunc( process_Normal_Keys);
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 32.0, 64.0, 0.0);
    //glutIdleFunc( idle);
    glutMainLoop();
    //void glutIdleFunc(void (*func)(void)
}

int main(int argc, char **argv) {
    //screen
    glutInit(&argc, argv);
    glutInitRoutines(); // initialisation for opengGL window
    //screen end
    char rom_name[] = "img Fishie.ch8";

    // #Start Chip8
    C8Klasse c8; // lege interpreter an
    c8.initSpeicher(); // initialisiere den Speicher zu 0
    // #Setup cartridge
    Cartridge testCartridge; // declare a Cartridge ( its the rom with the game data !!
    testCartridge.readInOpcodeFromFileInArray(rom_name); // reads opcode of a data-file (should be in workspace) into an intern array
    testCartridge.printOpcode();
    // # Cartridgecode to Chip8 Memory
    c8.setOpCode(testCartridge);// here the Opcode of the game is passed to the c8 CPU ( Cartridge ->internal memory )!
    c8.printMemory(); // here the c8 internal memory is printed out, should be same form 0x200 to 0xfff with cartridge , since the game has been read in !
    c8.printMemory(0x200); // prints memory by starting at given paramater address !

    cout << "\n please press enter to continue with the program !" << endl;
    cin.get();
    cout << "enter" << endl;
    unsigned short int counter = 0x00; // ein zykluscounter - zählt zeit bis zum nächsten unsigned short interrupt....
    unsigned short int opCode = 0;
    //##########################################################   main loop
    c8.setPC(0x200); // starte mit dem PC auf Adresse 0x200 !
    for (int i = 0; i < testCartridge.getOpcodeLength(); i++) {
        // opCOde is a unsigned short int ( 16 Bit ) the memory is just 8 Bit ! so please make some bit-amipulation: executableOpcode=(opcode1 <<8) | (opcode2)
        opCode = ((unsigned short int) ((c8.getMemAtPC() & (0xff))) << 8);
        c8.incrementPCCounter(1); // increment PC
        opCode = opCode | (((unsigned short int) (c8.getMemAtPC() & (0xff))));
        c8.incrementPCCounter(1); // increment PC -- for the next round
        c8.dissembler(opCode); // so if the opcode variable is fille with 2 Bytes please run the dissembler !
        cout << "\ncurrent pc: 0x" << hex << c8.getPC();
        //      cout << ", the read out OpCode is:0x" << hex << opCode; // show opcode.
        printf(" the read OpCode is: 0x%04x", opCode);
        cin.get();
        // man muss bei den Befehlen unterscheiden:  befehle fangen mit einer fixen Zahl an und haben dann variablen!
        // man muss deswegen zuerst schauen welche zahl am Anfang steht oder , man muss nach einer Zahlenkombinatuon suchen
        //Befehle: http://devernay.free.fr/hacks/chip8/C8TECH10.HTM
        //Hex to ASCII converter : https://www.rapidtables.com/convert/number/hex-to-ascii.html
    }
    return 0;
}
/*
 *
 All instructions are 2 bytes long and are stored most-significant-byte first.
 In memory, the first byte of each instruction should be located at an even addresses.
 3.1 - Standard Chip-8 Instructions
 00E0 - CLS
 00EE - RET
 0nnn - SYS addr
 1nnn - JP addr
 2nnn - CALL addr
 3xkk - SE Vx, byte
 4xkk - SNE Vx, byte
 5xy0 - SE Vx, Vy
 6xkk - LD Vx, byte
 7xkk - ADD Vx, byte
 8xy0 - LD Vx, Vy
 8xy1 - OR Vx, Vy
 8xy2 - AND Vx, Vy
 8xy3 - XOR Vx, Vy
 8xy4 - ADD Vx, Vy
 8xy5 - SUB Vx, Vy
 8xy6 - SHR Vx {, Vy}
 8xy7 - SUBN Vx, Vy
 8xyE - SHL Vx {, Vy}
 9xy0 - SNE Vx, Vy
 Annn - LD I, addr
 Bnnn - JP V0, addr
 Cxkk - RND Vx, byte
 Dxyn - DRW Vx, Vy, nibble
 Ex9E - SKP Vx
 ExA1 - SKNP Vx
 Fx07 - LD Vx, DT
 Fx0A - LD Vx, K
 Fx15 - LD DT, Vx
 Fx18 - LD ST, Vx
 Fx1E - ADD I, Vx
 Fx29 - LD F, Vx
 Fx33 - LD B, Vx
 Fx55 - LD [I], Vx
 Fx65 - LD Vx, [I]
 3.2 - Super Chip-48 Instructions
 00Cn - SCD nibble
 00FB - SCR
 00FC - SCL
 00FD - EXIT
 00FE - LOW
 00FF - HIGH
 Dxy0 - DRW Vx, Vy, 0
 Fx30 - LD HF, Vx
 Fx75 - LD R, Vx
 Fx85 - LD Vx, R
 * */

最佳答案

当您调用 glutMainLoop() 时,您的程序将进入一个循环,看起来有点像:

void glutMainLoop()
{
  while(true)
  {
    MSG msg = getWindowEvent();
    switch(msg)
    {
    case QUIT: exit(0); break; //< NOTE: method never returns!!
    /* snip */
    }
  }
}

这就是在调用 glutMainLoop() 后无法运行任何代码的原因。如果您只想运行一次 main 底部的代码(在应用程序启动时),那么只需将 glutMainLoop() 从您的例程设置函数中移出,使其成为 main() 中的最后一次调用。如果您想在更新 GL 窗口时同时运行该代码,您有两种选择:

  1. 只需在需要时运行 std::cin 代码(作为对按键等的响应)。它会导致 OpenGL 窗口在该代码运行时卡住,仅仅是因为它是一个单线程应用程序,所以在您完成之前不会发生重绘。
  2. 使用 glut keypress/keyrelease 回调将输入的文本填充到全局 std::string(一次按下一个键)。当您检测到输入时,解析和处理您的操作码形成全局字符串。不要调用 cin,并通过 OpenGL 在屏幕上呈现文本(或者,如果您非常懒惰,请调用 glutSetWindowTitle,这避免了查找代码以在屏幕上呈现字体的需要 - 尽管 glut 可以提供帮助!)
  3. 为基于 std::cin 的代码创建第二个线程(如果这是您真正想要做的事情?)。然后你必须处理线程的麻烦,在这种情况下恕我直言,这可能是矫枉过正。

就个人而言,我会选择选项 2。

关于c++ - 如何与opengl并行运行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59078257/

相关文章:

java - 是否有现成的可同时进行 cucumber 测试的基础设施?

c++ - 这个函数如何将文本读入结构?

c++ - C++命名空间的困扰

c++ - 什么保证存储在 char32_t 或 char16_t 中的 UTF-8 代码单元的 static_cast 到 char 会按预期结果?

c++ - 与 GLSL 相关的 OpenGL 应用程序代码

windows - 当窗口失去焦点或调整大小时如何阻止 OpenGL 暂停?

c# - Parallel.For 具有 64 位无符号索引 (UInt64)

c++ - 异常类因递归函数而失败

opengl - 如何在openGL中实现关节和骨骼?

python - 如何在 Python 中进行异步文件复制?