- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
原始问题:
I've been asked prior to a job interview to understand how an anti-aliased line is drawn in a framebuffer, using C or C++. I haven't used C, and it's been a few years for me since last using C++. I am a complete beginner when it comes to graphics. My C++ experience has mostly been in simple command-line programs and sorting methods. The company does not care if I grab the code online, they want me to understand it but still have a working executable.
I've used this tutorial to set up SDL libraries in MS VC++ 2012 Express, and this algorithm for the actual anti-aliasing. I have a good understanding of the algorithm, though I'm currently having trouble getting it to compile. I just want a line to be drawn, and then I can go forward with setting the code up to the skeleton class definitions I was given. This is what I have included aside from what is on that page with the algorithm:
#include <cmath>
#include <math.h>
#include "conio.h"
#include "stdlib.h"
#include "stdio.h"
#include "SDL.h"
const double HEIGHT = 240;
const double WIDTH = 320;
const double X0 = 25.6;
const double X1 = 64.7;
const double Y0 = 30;
const double Y1 = 42;
int round(double number)
{
return number < 0.0 ? ceil(number - 0.5) : floor(number + 0.5);
}
void main()
{
Uint32 pixelColor = 00000000000000000000000000000000;
SDL_Surface* myScreen = SDL_CreateRGBSurface(SDL_ALPHA_OPAQUE,WIDTH,HEIGHT,32, 0x000000FF,0x0000FF00, 0x00FF0000, 0xFF000000);
WULinesAlpha(X0, X1, Y0, Y1,pixelColor,myScreen);
return;
}I'm getting the following errors:
Error 21 error LNK2019: unresolved external symbol _SDL_main referenced in function _main Error 22 error LNK1120: 1 unresolved externals
I've seen a few code examples saying the main function has to look like this:
int main(int argc, char *argv[])
{
}Again, graphics stuff is unfamiliar to me so I know my main function is likely very wrong; I'm anticipating some shaking heads. Can someone explain what is happening/what I need to do?
新:
根据 NomNomNom069 的 YouTube 视频“C++ SDL 教程 2 创建屏幕和处理基本输入”,我现在已将我的主要功能替换为以下代码
#include "SDL.h"
int main(int argc, char * args[])
{
bool running = true;
//initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
running = false;
}
//set up screen
SDL_Surface *screen;
screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE);
if (screen == NULL)
{
running = false;
}
SDL_Event occur;
//main application loop
while (running)
{
SDL_PollEvent(&occur);
if (occur.type == SDL_QUIT)
{
running = false;
}
//drawing occurs here
SDL_FillRect(screen, NULL, 0);
SDL_Flip(screen);
}
//quit SDL
SDL_Quit();
return 0;
}
没有错误,我会弹出一个窗口。太棒了。
我现在的问题是关于如何/在何处调用 WuLinesAlpha
。此函数调用 4 个 double
、一个 Uint32
变量和一个 SDL_Surface*
。我有我的double
,我将Uint32
设置为0x000000FF
,我假设我设置的SDL_Surface
up as screen
是传入的。
我一直在研究 WuLinesAlpha
函数调用的去向,但总是出现黑屏。我以为,正如视频中所解释的那样,它会进入循环,但什么也没发生。我应该调用更多 SDL 命令吗?
最佳答案
首先修正你的 main
声明。这确实需要是 int main(int argc, char *argv[])
。特别是在 Windows 上,因为我相信 SDL.h
实际上会将您的 main
重命名为其他名称,并接管 main
库本身。
接下来,确保正确链接到 SDL。在我自己的基于 SDL 1.2.x 的项目中,我的 Makefile 中有这些行:
SDL_CFLAGS := $(shell sdl-config --cflags)
SDL_LFLAGS := $(shell sdl-config --libs)
然后我将这些标志附加到我的实际 CFLAGS
和 LFLAGS
中。请注意,如果您使用 make
和 Makefile
,您需要在那里使用 :=
,否则 make
将调用每次扩展 $(CFLAGS)
时 $(shell ...)
命令。
我无法帮助您设置 Microsoft 的 GUI 产品。本教程针对稍旧的 MSVC 产品(2010 年),看起来很不错,可能会让您走上正轨:http://lazyfoo.net/SDL_tutorials/lesson01/windows/msvsnet2010e/index.php
最后,不要忘记在某个时候调用 SDL_Init()
,最好是在开始创建表面之前。
祝你好运!
关于面向 SDL 初学者的 C++ 抗锯齿线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18173745/
我是一名优秀的程序员,十分优秀!