1. C++ / Говнокод #12827

    +24

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    void sleep_in_qt_ms(unsigned millisec) {
        QMutex foo;
        foo.lock();
        foo.try_lock(millisec);
        foo.unlock();
    }

    sleep в Qt - что, серьезно, чтоли?

    особенно порадовало: Warning: Destroying a locked mutex may result in undefined behavior.
    действительно, накой нам деструкторы?

    defecate-plusplus, 29 Марта 2013

    Комментарии (73)
  2. C++ / Говнокод #12820

    +11

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    void Bitmap::flipRB()
    {
    	int nBytes = w*h;
    	void *d = data;
    	__asm
    	{
    		mov edx, 0;
    pixloop:	mov eax, d;
    		mov eax, [eax];
    		mov ebx, edx;
    		shl ebx, 0x2;
    		add eax, ebx;
    		mov ebx, [eax];
    		
    		and ebx, 0xFFFFFF;
    		mov ecx, ebx;
    		shr ebx, 0x10;
    		shl ecx, 0x10;
    		or ebx, ecx;
    		shr ecx, 0x10;
    		and ecx, 0xFF00;
    		or ebx, ecx;
    		and ebx, 0xFFFFFF;
    		or ebx, 0xFF000000;
    		
    		mov [eax], ebx;
    		inc edx;
    		cmp edx, nBytes;
    		jne pixloop;
    	}
    }

    Откопал очередной свой велосипедный класс, в нем попался такой вот метод для конвертирования цветов ARGB в ABGR.

    just_nameless, 28 Марта 2013

    Комментарии (10)
  3. C++ / Говнокод #12815

    +3

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    #include <cstdlib>
    #include <iostream>
    #include <time.h>
    
    using namespace std;
    
    const int n=20;
    int mass[20];
    
    void massiv(int x[])
    {
    srand(time(NULL));
    for(int i=0;i<20;i++) mass[i]=rand()%100;
    return 0;
    }
    
    void vivod(int x[])
    {
    for(int i=0;i<20;i++) cout<<mas[i]<<"\n";
    return 0;
    }
    
    
    void sort (int x[])
    {
    int i,j,b;
    for(j=1;j<=n-1;j++)
    for(i=0;i<=n-1-j;i++)
    if (mass[i]>mass[i+1])
    {
    b=mass[i];
    mass[i]=mass[i+1];
    mass[i+1]=b;
    }
    }
    return 0;
    }
    
    int main(int argc, char *argv[])
    {
        zapolneniemassiva(mass[20]);
        pokaz(mass[20]);
    	sort(mass[20]);
    	variant3(mass[20]);
    	
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    Одногруппник сделал лабу на C++, программа сортирует массив по возрастанию и выводит, ну, по крайней мере, должна.

    TBoolean, 28 Марта 2013

    Комментарии (86)
  4. C++ / Говнокод #12812

    +1

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    int main()
    {
    	__asm()
    	{
    	NOP
    	}
    	return 0;
    }

    программа делает ничего, но делает

    tsovak, 28 Марта 2013

    Комментарии (4)
  5. C++ / Говнокод #12811

    +11

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    typedef ::boost::array<T, item_amount+1> not_raw_array;
    union data
    {
    	 not_raw_array m_raw_array;//Most members of boost::array not suitable for call with raw array. Be careful!
    };

    LispGovno, 27 Марта 2013

    Комментарии (16)
  6. C++ / Говнокод #12806

    +20

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    int spectrumColor(float magn)
    {
    	if (magn != magn) return 0xFF000000;
    	if ((magn <= -FLT_MAX && magn >= FLT_MAX)) return 0xFF000000;
    	const float minRange = -95.0f;
    	if (magn > minRange && magn <= (minRange + 10.0f))
    		return blendColor(0, 0x00004f, (magn - minRange) / 10.0f);
    	if (magn > (minRange + 10.0f) && magn <= (minRange + 20.0f))
    		return blendColor(0x00004f, 0x50007b, (magn - (minRange + 10.0f)) / 10.0f);
    	if (magn > (minRange + 20.0f) && magn <= (minRange + 30.0f))
    		return blendColor(0x50007b, 0x990076, (magn - (minRange + 20.0f)) / 10.0f);
    	if (magn > (minRange + 30.0f) && magn <= (minRange + 40.0f))
    		return blendColor(0x990076, 0xd20040, (magn - (minRange + 30.0f)) / 10.0f);
    	if (magn > (minRange + 40.0f) && magn <= (minRange + 50.0f))
    		return blendColor(0xd20040, 0xf51f00, (magn - (minRange + 40.0f)) / 10.0f);
    	if (magn > (minRange + 50.0f) && magn <= (minRange + 60.0f))
    		return blendColor(0xf51f00, 0xffaa00, (magn - (minRange + 50.0f)) / 10.0f);
    	if (magn > (minRange + 60.0f) && magn <= (minRange + 70.0f))
    		return blendColor(0xffaa00, 0xfff966, (magn - (minRange + 60.0f)) / 10.0f);
    	if (magn > (minRange + 70.0f) && magn <= (minRange + 80.0f))
    		return blendColor(0xfff966, 0xffffff, (magn - (minRange + 70.0f)) / 10.0f);
    	if (magn < minRange) return 0xFF000000;
    	if (magn >= (minRange + 80.0f)) return 0xFFFFFFFF;
    	return 0xFF000000;
    }

    Вот так в одной говноподелке считается цвет столбцов спектра для визуализации аудио.

    just_nameless, 25 Марта 2013

    Комментарии (102)
  7. C++ / Говнокод #12805

    +24

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    tmpPtr = SCPGetFreeTextMedicalHistory(ecg);
    if (!tmpPtr) tmpPtr = unsp;
    m_patientPage->SetDlgItemTextW(IDC_EDIT15, tmpPtr);
    if (tmpPtr != unsp) SCPFreeMem(tmpPtr);
    
    tmpPtr = SCPGetTechnicianDescription(ecg);
    if (!tmpPtr) tmpPtr = unsp;
    m_miscPage->SetDlgItemTextW(IDC_EDIT19, tmpPtr);
    if (tmpPtr != unsp) SCPFreeMem(tmpPtr);
    
    . . .
    
    tmpPtr = SCPGetLatestConfirmingPhysician(ecg);
    if (!tmpPtr) tmpPtr = unsp;
    m_miscPage->SetDlgItemTextW(IDC_EDIT18, tmpPtr);
    if (tmpPtr != unsp) SCPFreeMem(tmpPtr);
    
    tmpPtr = SCPGetReferringPhysician(ecg);
    if (!tmpPtr) tmpPtr = unsp;
    m_miscPage->SetDlgItemTextW(IDC_EDIT17, tmpPtr);
    if (tmpPtr != unsp) SCPFreeMem(tmpPtr);
    
    . . .

    just_nameless, 25 Марта 2013

    Комментарии (8)
  8. C++ / Говнокод #12802

    +9

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    void drawAAWave(DWORD *image, WAVE *w, int top, int left, 
    		int width, int height, int offsetPx)
    	{
    		int thd2 = top + height/2;
    		float hm2 = this->height - 2;
    		int x = 0, xc = 0, yc = 0;
    		float mul = amplify;
    		if (invert) mul = -mul;
    		if (x*scale + offsetPx*scale > w->length) return;
    		float yFrom = (thd2 - w->data[offsetPx*scale]*mul);
    		if (yFrom < 2.0f) yFrom = 2.0f;
    		if (yFrom > hm2) yFrom = hm2;
    		float yTo, dist, delta, sdelta, alpha, lc; 
    		int yMin, yMax, xcl, n;
    		for ( ; x < width-1 && (x+offsetPx)*scale < w->length; x++)
    		{
    			yTo = thd2 - w->data[(x + offsetPx)*scale]*mul;
    			if (yTo < 2) yTo = 2; if (yTo > hm2) yTo = hm2;
    			delta = yTo - yFrom;
    			sdelta = 1.0f / sqrtf(1.0f + delta*delta);
    			lc = ((x + 0.5f)*yTo - (x + 1.5f)*yFrom) + 0.5f;
    			yMin = (int)(min(yFrom, yTo)) - 1;
    			yMax = (int)(max(yFrom, yTo)) + 1;
    			xcl = x;
    			if (delta > 1.0f || delta < -1.0f) xcl++;
    			for (xc = x; xc <= xcl; xc++)
    				for (yc = yMin; yc <= yMax; yc++)
    				{
    					dist = ((-delta)*(xc + 0.5f) + yc + lc)*sdelta;
    					alpha = 1.0f - fabs(dist*0.8f);
    					if (alpha < 0.0f) alpha = 0.0f;
    					n = maxWidth*yc + xc;
    					image[n] = clrBlend(image[n], lineColor, alpha);
    				}
    			yFrom = yTo;
    		}
    	}

    Велосипедный метод рисования кривой с антиализингом

    just_nameless, 25 Марта 2013

    Комментарии (3)
  9. C++ / Говнокод #12789

    +13

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    CompoundExpression*
    CompoundExpression::newBinaryExpression(
    	BasicBinaryOperation::Type operation,
    	const Expression *x,
    	const Expression *y
    ) {
    	vector<Expression::const_pointer> params(2);
    	params[0] = x;
    	params[1] = y;
    	
    	// integer power optimization
    	if (operation == BasicBinaryOperation::POWER) {
    		if (y->isNumber()) {
    			Number::const_pointer number_y = dynamic_cast<typeof number_y>(y);
    			if (number_y != NULL && number_y->isIntegerNumber()) {
    				IntegerNumber::const_pointer integer_y = dynamic_cast<typeof integer_y>(number_y);
    				if (integer_y != NULL) {
    					operation = BasicBinaryOperation::INT_POWER;
    					return new CompoundExpression(BinaryOperation::getOperation(operation), params);
    				}
    			}
    		}
    	}
    	
    	// x^(y/n), where 'n' is odd integer
    	// transform to '(x^y)^(1/n)'
    	if (operation == BasicBinaryOperation::POWER) {
    		if (y->isCompoundExpression()) {
    			auto compoundExpressionY = dynamic_cast<CompoundExpression::const_pointer>(y);
    			if (compoundExpressionY != NULL && compoundExpressionY->operation->isBinary()) {
    				auto innerOperation = compoundExpressionY->operation;
    				auto binaryOperation = dynamic_cast<BinaryOperation const *>(innerOperation);
    				if (binaryOperation != NULL && binaryOperation->getType() == BasicBinaryOperation::DIVIDE) {
    					Expression::const_pointer   numerator = compoundExpressionY->params[0];
    					Expression::const_pointer denominator = compoundExpressionY->params[1];
    					if (denominator->isNumber()) {
    						auto numberDenominator = dynamic_cast<Number::const_pointer>(denominator);
    						if (numberDenominator != NULL && numberDenominator->isIntegerNumber()) {
    							auto integerDenominator = dynamic_cast<IntegerNumber::const_pointer>(numberDenominator);
    							if (integerDenominator != NULL && (integerDenominator->intValue() % 2) != 0) {
    								auto base = CompoundExpression::newBinaryExpression(BasicBinaryOperation::POWER, x, numerator);
    								return CompoundExpression::newBinaryExpression(BasicBinaryOperation::NTH_ROOT, integerDenominator, base);
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    
    	return new CompoundExpression(BinaryOperation::getOperation(operation), params);
    }

    Моё. Потребовалось воткнуть оптимизацию арифметического выражения некоторого вида. В результате родился вот такой костыль.

    UncleAli, 24 Марта 2013

    Комментарии (4)
  10. C++ / Говнокод #12770

    +19

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    struct BufInfo 
    {
    	const tbal::Bitmap &src, &dst;
    	int y1, y2;
    	BufInfo (const tbal::Bitmap &scr, const tbal::Bitmap &dst, int y1, int y2) : src(src), dst(dst), y1(y1), y2(y2) {}
    };

    Как можно проебать час жизни...

    TarasB, 20 Марта 2013

    Комментарии (51)