gpt4 book ai didi

c++ - 构建和运行应用程序时出现奇怪的噪音

转载 作者:搜寻专家 更新时间:2023-10-31 01:00:23 24 4
gpt4 key购买 nike

我正在学习 SDL 2.0 教程,因为自从我学习 1.2 后情况发生了很大变化。我现在有一个非常简单的程序,只有一个窗口类、一个应用程序类和一个纹理类。当我调试和编译我的程序时,我从我的扬声器中听到类似高音调的嗡嗡声。如果我从调试文件夹运行已编译的 .exe 文件,也会发生这种情况。

我不知道如何解决这个问题。因为它似乎只是我的程序,并且发生在 .exe 文件上,所以我怀疑这是 visual studio 的错。虽然我不是真正的大程序员,但这只是一种爱好,所以也许是。这对我的程序来说是良性的(到目前为止,因为它很小),但它很烦人。有人知道为什么我的扬声器开始发出噪音吗?

提前谢谢你。

我将在下面发布我的代码,但我没有对 SDL Audio API 做任何事情。

源文件

#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() {}

KWindow.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);
}
}

最佳答案

当您的应用程序以非常高的帧速率运行时,我已经看到了这种情况。尖锐的声音通常来自显卡。

尝试在代码中启用垂直同步或限制应用程序的帧率。

关于c++ - 构建和运行应用程序时出现奇怪的噪音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31215266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com