2015-07-03 20 views
2

由于我学习了1.2版,事情发生了很大变化,因此我正在通过SDL 2.0教程进行滚动。我现在有一个非常简单的程序,只是一个窗口类,一个应用程序类和一个纹理类。当我调试和编译我的程序时,我听到这个声音像我的扬声器发出的高嗡嗡声。如果我从debug文件夹运行编译后的.exe文件,也会发生这种情况。当我构建并运行应用程序时发生奇怪的噪音

我不知道如何解决这个问题。由于它似乎只是我的程序,并发生在.exe文件中,所以我怀疑这是Visual Studio的错。虽然我不是一个真正的大型程序员,但它只是一种业余爱好,所以也许就是这样。这对我的程序来说是温和的(到目前为止,因为它很小),但它很烦人。任何人都有一个想法,为什么我的扬声器刚开始喷出噪音?

预先感谢您。

我会在下面发布我的代码,但是我对SDL Audio API没有任何帮助。

src.cpp

#include <Windows.h> 
#include "KApp.h" 


const int winW = 800; 
const int winH = 600; 


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

    KApp ThisApp; 

    ThisApp.Run(winW, winH); 

    return 0; 
} 

KApp.h

#ifndef __KAPP_H__ 
#define __KAPP_H__ 

#include <SDL.h> 
#include <vector> 

#include "Window.h" 
#include "KTexture.h" 


class KApp { 

private: 

    enum States { 
     INIT = 0, 
     RUNNING, 
     PAUSED, 
     HALTED, 

    }; 

    States GameState; 
    Window* MainWindow; 
    std::vector<KTexture*> Textures; 

public: 

    KApp(); 
    ~KApp(); 

    void Run(const int &,const int &); 
    void Handle(SDL_Event* Event); 
    void Update(); 
    void Render(); 

    void CleanUp(); 

}; 



#endif 

KApp.cpp

#include "KApp.h" 


KApp::KApp() { 

    GameState = INIT; 
    MainWindow = NULL; 

    if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { 
     exit(0x1); 
    } 

    //SDL_GL_SetAttribute(SDL_GL_RED_SIZE,   8); 
    //SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,   8); 
    //SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,   8); 
    //SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,   8); 

    //SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,  16); 
    //SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,  32); 

    //SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,  8); 
    //SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8); 
    //SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8); 
    //SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8); 

    //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); 
    //SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2); 

    return; 

} 

KApp::~KApp() { 

    if(MainWindow != NULL) { 
     delete MainWindow; 
     MainWindow = NULL; 
    } 

    for(std::vector<KTexture*>::iterator it = Textures.begin(); it != Textures.end(); it++) { 
     delete *it; 
    } 
    Textures.clear(); 

    SDL_Quit(); 

    return; 
} 

void KApp::Handle(SDL_Event* Event) { 
    if(Event->type == SDL_QUIT) { 
     GameState = HALTED; 
    } 
} 

void KApp::CleanUp() { 

} 

void KApp::Render() { 
    MainWindow->Render(); 
} 


void KApp::Run(const int& w,const int& h) { 

    MainWindow = new Window(w,h,"KApp Main Window"); 
    if(!MainWindow->Create()) { 
     exit(1); 
    } 

    Textures.push_back(new KTexture("E:/Pictures/BKG.bmp")); 
    Textures[0]->Optimize(MainWindow->Surface()); 

    MainWindow->Blit(Textures[0]->Surface()); 

    GameState = RUNNING; 
    SDL_Event Event; 

    while(GameState == RUNNING) { 
     while(SDL_PollEvent(&Event)) { 
      Handle(&Event); 
     } 

     Update(); 
     Render(); 
    } 

    CleanUp(); 

} 

void KApp::Update() {} 

ķ在window.h

#ifndef __WINDOW_H__ 
#define __WINDOW_H__ 

#include <SDL.h> 
#include <gl/gl.h> 
#include <gl/glu.h> 
#include <string> 


class Window { 

private: 
    int Width; 
    int Height; 
    std::string Title; 

    SDL_Window* WNDW; 
    SDL_Surface* SFC; 


public: 
    Window() : Width(800),Height(600),Title("Window"),WNDW(NULL),SFC(NULL) {} 
    Window(int w, int h, std::string title) : Width(w),Height(h),Title(title),WNDW(NULL),SFC(NULL) {} 
    ~Window() { 
     if(WNDW != NULL) { 
      SDL_DestroyWindow(WNDW); 
      WNDW = NULL; 
     } 

    } 

    bool Create() { 

     if(WNDW != NULL) { 
      return true; 
     } 

     WNDW = SDL_CreateWindow(Title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Width, Height, SDL_WINDOW_SHOWN); 
     if(WNDW == NULL) { 
      return false; 
     } 

     SFC = SDL_GetWindowSurface(WNDW); 
     SDL_FillRect (SFC , NULL, SDL_MapRGB(SFC->format, 0x00, 0x00, 0x00)); 
     SDL_UpdateWindowSurface(WNDW); 


     //if((Surface = SDL_SetVideoMode(Width,Height,32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL | SDL_RESIZABLE)) == NULL) { 
     // return false; 
     //} 
     /* 
     glClearColor(0, 0, 0, 0); 
     glClearDepth(1.0f); 
     glViewport(0, 0, Width, Height); 
     glMatrixMode(GL_PROJECTION); 
     glLoadIdentity(); 
     glOrtho(0, Width, Height, 0, 1, -1); 
     glMatrixMode(GL_MODELVIEW); 
     glEnable (GL_BLEND); 
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
     glLoadIdentity(); 
     */ 

     return true; 
    } 

    void Clear() { 
     SDL_FillRect (SFC , NULL, SDL_MapRGB(SFC->format, 0x00, 0x00, 0x00)); 
     SDL_UpdateWindowSurface(WNDW); 
    } 

    void Blit(SDL_Surface* Surf) { 
     SDL_BlitSurface(Surf, NULL, SFC, NULL); 
    } 

    void BlitScaled(SDL_Surface* Surf, int x, int y, int w, int h) { 
     SDL_Rect MyRectum; 
     MyRectum.x = x; 
     MyRectum.y = y; 
     MyRectum.w = w; 
     MyRectum.h = h; 
     SDL_BlitScaled(Surf, NULL, SFC, &MyRectum); 
    } 

    void Render() { 
     SDL_UpdateWindowSurface(WNDW); 
    } 

    SDL_Surface* Surface() { 
     return SFC; 
    } 

}; 

#endif 

KTexture.h

#ifndef __KTEXTURE_H__ 
#define __KTEXTURE_H__ 

#include <SDL.h> 
#include <string> 

class KTexture { 

private: 
    SDL_Surface* Texture; 

public: 

    KTexture() : Texture(NULL) {} 
    KTexture(std::string path) : Texture(NULL) { 
     Load(path); 
    } 

    ~KTexture() { 
     if(Texture != NULL) { 
      SDL_FreeSurface(Texture); 
      Texture = NULL; 
     } 
    } 

    bool Load(std::string path) { 

     Texture = SDL_LoadBMP(path.c_str()); 
     if(Texture == NULL) { 
      return false; 
     } 

     return true; 
    } 

    void Render() { 
     Render(0,0); 
    } 

    void Render(int x, int y) { 


    } 

    SDL_Surface* Surface() { 
     return Texture; 
    } 

    bool Optimize(SDL_Surface* For) { 
     SDL_Surface* nsurf = SDL_ConvertSurface(Texture, For->format, NULL); 
     if(nsurf == NULL) { 
      return false; 
     } 

     SDL_FreeSurface(Texture); 
     Texture = nsurf; 

     return true; 

    } 

}; 


#endif 

编辑固定。添加到KApp :: Run()函数

Uint32 Time = SDL_GetTicks(); 

GameState = RUNNING; 
SDL_Event Event; 

while(GameState == RUNNING) { 
    while(SDL_PollEvent(&Event)) { 
     Handle(&Event); 
    } 

    Update(); 
    if(SDL_GetTicks() > Time + 300) { 
     Render(); 
     Time = SDL_GetTicks(); 
    } 
    else 
    { 
     SDL_Delay(1); 
    } 
} 
+0

程序运行时是否检查CPU负载? – rici

+0

我做了,它是30%,但帧率限制固定的噪音。 在此处添加睡眠可以修复CPU使用率。 – Chemistpp

回答

4

我看到这种情况发生时,你有一个应用程序运行在一个非常高的帧率。高音通常来自显卡。

尝试在代码中启用vsync或限制应用程序的帧率。

相关问题