Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        22天前关闭。
                                                                                            
                
        
我想运行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窗口的同时运行该代码,则有两种选择:


只需在需要时运行std :: cin代码(作为对按键的响应等)。运行该代码时,这将导致OpenGL窗口冻结,这仅仅是因为它是一个单线程应用程序,因此重绘要等到完成后才能进行。
使用glut keypress / keyrelease回调用输入的文本填充全局std :: string(一次按一次键)。当您检测到enter时,将解析并处理您的操作码,形成全局字符串。不要调用cin并通过OpenGL在屏幕上渲染文本(或者,如果您非常懒惰,请调用glutSetWindowTitle,这样可以避免查找代码以在屏幕上渲染字体的需求-尽管glut可以帮到您!)
为基于std :: cin的代码创建第二个线程(如果您确实要这样做?)。然后,您必须处理线程的麻烦,在这种情况下,恕我直言,这可能是过大的了。


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

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

10-11 23:00
查看更多