C++ SDL在屏幕上放置像素C++;
我从SDL开始,我正在阅读介绍,我正在尝试他们的C++ SDL在屏幕上放置像素C++;,c++,sdl,pixel,ppm,C++,Sdl,Pixel,Ppm,我从SDL开始,我正在阅读介绍,我正在尝试他们的drawPixel方法。我正在做的是一个ppm查看器,到目前为止,我在一个数组中有rgb值并正确存储(我通过打印数组并确保它们与ppm文件中的位置相对应来检查它们),我想使用SDL来绘制图片。到目前为止,我写的代码是(这是main.cpp文件,如果需要ppm.hpp和ppm.cpp,请告诉我如何添加它们) #包括 #包括 #包括“ppm.hpp” 使用名称空间std; void drawPixel(SDL_曲面*,Uint8,Uint8,Uint8
drawPixel
方法。我正在做的是一个ppm查看器,到目前为止,我在一个数组中有rgb值并正确存储(我通过打印数组并确保它们与ppm文件中的位置相对应来检查它们),我想使用SDL来绘制图片。到目前为止,我写的代码是(这是main.cpp
文件,如果需要ppm.hpp
和ppm.cpp
,请告诉我如何添加它们)
#包括
#包括
#包括“ppm.hpp”
使用名称空间std;
void drawPixel(SDL_曲面*,Uint8,Uint8,Uint8,int,int);
int main(int argc,字符**argv){
PPM PPM(“res/cake.PPM”);
中频(SDL_Init(SDL_Init_音频| SDL_Init_视频)<0){
cerr格式->Rshift/8)=R;
*(bufp+屏幕->格式->Gshift/8)=G;
*(bufp+屏幕->格式->B移位/8)=B;
}
打破
案例4:{//可能为32 bpp
Uint32*bufp;
bufp=(Uint32*)屏幕->像素+y*屏幕->基音/4+x;
*bufp=颜色;
}
打破
}
如果(SDL_必须锁定(屏幕)){
SDL_解锁表面(屏幕);
}
SDL_(屏幕,x,y,1,1);
}
drawPixel
与简介中提供的一样,现在我尝试使用的ppm文件名为cake.ppm
及其720x540,但是当我构建并运行此代码时,我发现应用程序没有响应。我在一个较小的ppm文件426x299上试用了它,它显示了一个窗口,窗口上有颜色
cake.ppm
文件和其他文件上工作?是因为尺寸吗squares.ppm
,应该是这样的:
但这就是我得到的
使用cake.ppm运行时,ppm.height()和ppm.width()的值是多少?ppm.height()是540,ppm.width()是720。您是否尝试过使用调试器检查应用程序在进入“无响应”状态时挂起的位置?另外:试着在图像中画一个你可以很容易识别的结构,看看它是如何显示的(例如,你可以使用一个有不同颜色手臂的十字架)。颜色正确吗?结构的位置正确吗?如果你刚开始使用SDL,我建议你使用SDL_图像,而不是自己绘制。
#include <iostream>
#include <SDL/SDL.h>
#include "ppm.hpp"
using namespace std;
void drawPixel (SDL_Surface*, Uint8, Uint8, Uint8, int, int);
int main (int argc, char** argv) {
PPM ppm ("res/cake.ppm");
if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0) {
cerr << "Unable to init SDL: " << SDL_GetError() << endl;
exit(1);
}
atexit(SDL_Quit); // to automatically call SDL_Quit() when the program terminates
SDL_Surface* screen;
screen = SDL_SetVideoMode(ppm.width(), ppm.height(), 32, SDL_SWSURFACE);
if (screen == nullptr) {
cerr << "Unable to set " << ppm.width() << "x" << ppm.height() << " video: " << SDL_GetError() << endl;
exit(1);
}
for (int i = 0; i < ppm.width(); i++) {
for(int j = 0; j < ppm.height(); j++) {
drawPixel(screen, ppm.red(i,j), ppm.green(i,j), ppm.blue(i,j), i, j);
}
}
return 0;
}
void drawPixel (SDL_Surface* screen, Uint8 R, Uint8 G, Uint8 B, int x, int y) {
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
if (SDL_MUSTLOCK(screen)) {
if (SDL_LockSurface(screen) < 0) {
return;
}
}
switch (screen->format->BytesPerPixel) {
case 1: { // Assuming 8-bpp
Uint8* bufp;
bufp = (Uint8*)screen->pixels + y * screen->pitch + x;
*bufp = color;
}
break;
case 2: { // Probably 15-bpp or 16-bpp
Uint16 *bufp;
bufp = (Uint16*)screen->pixels + y * screen->pitch / 2 + x;
*bufp = color;
}
break;
case 3: { // Slow 24-bpp mode, usually not used
Uint8* bufp;
bufp = (Uint8*)screen->pixels + y * screen->pitch + x;
*(bufp + screen->format->Rshift / 8) = R;
*(bufp + screen->format->Gshift / 8) = G;
*(bufp + screen->format->Bshift / 8) = B;
}
break;
case 4: { // Probably 32-bpp
Uint32* bufp;
bufp = (Uint32*)screen->pixels + y * screen->pitch / 4 + x;
*bufp = color;
}
break;
}
if (SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
SDL_UpdateRect(screen, x, y, 1, 1);
}